Click here to Skip to main content
12,395,871 members (64,279 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 20:08pm
Updated 6-May-11 22:07pm
Dalek Dave433.4K
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,
Smile | :)
  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


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