Return distinctive IQueryable using LINQ?

I am writing a function that retrieves records from a database using LINQ to get IQueryable. This LINQ statement will pull out all records for active users for a certain period of time, and then spit the username, first name and last name on the Telerik RadGrid.

My problem is trying to get a distinctive value on the user id when you pull this data. I tried to rework this code to get the result. Here is an example of code that pulls all the data, while Distinct NOT does not work.

public static IQueryable GetActiveEmployees_Grid(string Period) { DataContext Data = new DataContext(); var Employees = (from c in DataSystem_Records where c.Period == Period orderby c.DataSystem_Employees.LName select c).Distinct(); return Employees; } 

After applying the DataSource to my Grid, this returns the user 4 times, one instance for each record for this period.

alt text

Is there a way to apply Distinct to my LINQ function to make this work the way I intend it?

+12
c # linq distinct
Dec 17 '10 at 16:07
source share
7 answers

The easiest way I found this is to use a group and then select the first one.

 public static IQueryable GetActiveEmployees_Grid(string Period) { DataContext Data = new DataContext(); var Employees = (from c in DataSystem_Records where c.Period == Period orderby c.DataSystem_Employees.LName select c).GroupBy(g=>g.DataSystem_Employees.AccID).Select(x=>x.FirstOrDefault()); return Employees; } 

This is not tested, but there is a general concept.

Change I remember originally finding the answer somewhere here. Check this to group objects by a specific property. LINQ's Distinct () for a specific property

+19
Dec 17 '10 at 16:17
source share

If you restrict the objects you return to only the fields you want to display, they will work correctly.

 public static IQueryable GetActiveEmployees_Grid(string Period) { DataContext Data = new DataContext(); var Employees = (from c in DataSystem_Records where c.Period == Period orderby c.DataSystem_Employees.LName select c.DataSystem_Employees.FName, c.DataSystem_Employees.LName, c.ID).Distinct(); return Employees; } 
+4
Dec 17 '10 at 16:09
source share

try writing IEqualityComparer<T> for the selected object type and use it in the Distinct method

+4
Dec 17 '10 at 16:10
source share

Making some assumptions around the names of various fields in objects:

 public static IQueryable GetActiveEmployees_Grid(string Period) { DataContext Data = new DataContext(); var Employees = (from c in DataSystem_Records where c.Period == Period orderby c.DataSystem_Employees.LName select new { FirstName = c.DataSystem_Employees.FName, LastName = c.DataSystem_Employees.LName, ID = c.DataSystem_Employees.ID }).Distinct(); return Employees; } 

To follow the MVC pattern, you can raise this query into your model and return a specific class containing these fields.

+2
Dec 17 '10 at 16:11
source share

To do this, use the Distinct () method. For example:

 var query = from Notification in db.Notifications select Notification.client ; query=query.Distinct(); 

The resulting query will contain only individual values.

0
Jan 22 '18 at 4:43
source share

The problem is that you are dropping fields that will make each row different. As sgriffinusa said, only cancel the 3 values ​​you show.

-one
Dec 17 '10 at 16:14
source share

You might want to implement a custom comparison method for the Distinct method. See the previous SO question here .

-one
Dec 17 '10 at 16:16
source share



All Articles