Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# LINQ
Hi Guys,
 
I want to get the data from database by LINQ TO SQL FIle,
 
In my Student Table, there are three columns :school, department and Name.
I want to select the distinct student's department through the student's school's name that I have known.
 
My code is :
DataClassesDataContext d = new DataClassesDataContext();
 
        IQueryable rets = (from p in d.Students
                                              where p.School=="Computer"
                                    select p).Distinct();
 
But it does not work well ,it will return a heap of data which is not distinct!
What should I do?
Posted 6-May-11 20:08pm
Edited 6-May-11 22:07pm
Dalek Dave432.5K
v2
Comments
Dalek Dave at 7-May-11 4:08am
   
Edited for Grammar and Readability.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

How about select p.department rather than just select p.
 
select p will give you all p's where any of p.school, p.department or p.name differ - so 2 students with different names in the same school / department are distinct - if you just want distinct departments select p.department which will just give you the department.
  Permalink  
Comments
Dalek Dave at 7-May-11 4:08am
   
Good call.
Prasanta_Prince at 7-May-11 7:57am
   
Good one
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

THANKS FRIEND,
but for instance i need to remove Same records from the result:
 
All result:
name department school
A D S
A D S
B D S
 

 
the result should be after using DISTINCT:
 
name department school
A D S
B D S
 

that's all...
  Permalink  
Comments
yesotaso at 7-May-11 4:31am
   
What does it have to do with performance? Besides if you include primary key, which happens to be uniques in the table, in distinct call you guarantee that all records are selected... Filter fields which set of fields you mean to be distinct and select them not whole record. For instance "...select p.Name, p.Department, p.School).Disticnt()" See above solution it is acceptable imo.
NuttingCDEF at 7-May-11 6:32am
   
So which fields are you also including in your result set that means the records you see are distinct? What other fields do you have in the Students table? If you include the whole record in the results then ANY field that differs between records will prevent Distinct from merging the records.
 
Are you ***SURE*** the relevant values in your records are distinct? No extra leading / trailing spaces or extra spaces in the middle? Same capitalisation? No spelling errors?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Hi it-discovery,
 
Try to change your code with this one :
 
IQueryable rets = (from p in d.Students
                   where p.School == "Computer"
                   select p.department).Distinct();
 

I hope this help,
Smile | :)
  Permalink  
v2
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

I am also facing these type of issues
Quote:
var Query = (from Q in ListCompanyObj
select new
{
CompanyID = Q.InvoiceID,
CompanyName = Q.CompanyName
}).Distinct().ToList();

Can anyone help me...........
  Permalink  
Comments
__TR__ at 14-Dec-12 13:22pm
   
This is not an answer. Use this[^] page to ask your question so that it will be visible to everyone and delete this solution to avoid getting downvoted.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 OriginalGriff 6,649
1 Sergey Alexandrovich Kryukov 6,270
2 CPallini 5,190
3 George Jonsson 3,574
4 Gihan Liyanage 2,522


Advertise | Privacy | Mobile
Web01 | 2.8.140916.1 | Last Updated 14 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100