Click here to Skip to main content
12,950,090 members (40,917 online)
Rate this:
 
Please Sign up or sign in to vote.
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 :
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
Updated 6-May-11 22:07pm
Dalek Dave433.3K
v2
Comments
Dalek Dave 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 7-May-11 4:08am
   
Good call.
Prasanta_Prince 7-May-11 7:57am
   
Good one
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,
:)
  Permalink  
v2

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

    Print Answers RSS
Top Experts
Last 24hrsThis month
OriginalGriff 5,829
CHill60 3,460
Maciej Los 2,953
Jochen Arndt 1,975
ppolymorphe 1,820


Advertise | Privacy | Mobile
Web02 | 2.8.170525.1 | Last Updated 23 Oct 2015
Copyright © CodeProject, 1999-2017
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