Returning a Distinct IQueryable with LINQ?

后端 未结 7 1510
無奈伤痛
無奈伤痛 2020-12-29 09:29

I\'m writing a Function that pulls Records from a DataBase using LINQ to get an IQueryable. This LINQ statement will pull all of the records for Active users within a certai

相关标签:
7条回答
  • 2020-12-29 10:15

    The problem is you are pulling back fields that will make each row distinct. Like sgriffinusa said, only pull back the 3 values you are displaying.

    0 讨论(0)
  • 2020-12-29 10:16

    try and write an IEqualityComparer<T> for the object type being selected and use it in your Distinct method

    0 讨论(0)
  • 2020-12-29 10:21

    If you limit the objects you are returning to only the fields that you want to display, it will work properly.

    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;
    }
    
    0 讨论(0)
  • 2020-12-29 10:24

    Making some assumptions around the names of the various fields in the 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 might want to lift this query into your Model and return a specific class that contains those fields.

    0 讨论(0)
  • 2020-12-29 10:25

    Use the Distinct() method to do this. Eg:

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

    The resulting query will only contain distinct values.

    0 讨论(0)
  • 2020-12-29 10:26

    Simplest way I have found to do this with object is using the groupby then selecting the first.

    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 the general concept is there.

    Edit: I remembered originally finding the answer somewhere on here. Check out this for grouping objects by a certain property. LINQ's Distinct() on a particular property

    0 讨论(0)
提交回复
热议问题