Click here to Skip to main content
11,920,211 members (49,857 online)
Rate this:
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 21:08pm
Edited 6-May-11 23:07pm
Dalek Dave433.2K
Dalek Dave 7-May-11 4:08am
Edited for Grammar and Readability.
Rate this: bad
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.department or 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.
Dalek Dave 7-May-11 4:08am
Good call.
Prasanta_Prince 7-May-11 7:57am
Good one
Rate this: bad
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 | :)

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

  Print Answers RSS
0 OriginalGriff 455
1 phil.o 170
2 CPallini 150
4 F-ES Sitecore 130
0 OriginalGriff 6,778
1 KrunalRohit 4,495
2 Sergey Alexandrovich Kryukov 3,203
3 George Jonsson 2,805
4 Suvendu Shekhar Giri 2,181

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