Click here to Skip to main content
15,886,061 members
Please Sign up or sign in to vote.
1.82/5 (4 votes)
See more:
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 :
C#
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
Updated 6-May-11 22:07pm
v2
Comments
Dalek Dave 7-May-11 4:08am    
Edited for Grammar and Readability.

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.
 
Share this answer
 
Comments
Dalek Dave 7-May-11 4:08am    
Good call.
Prasanta_Prince 7-May-11 7:57am    
Good one
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,
:)
 
Share this answer
 
v2

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900