Returning a Distinct IQueryable with LINQ?

后端 未结 7 1710
無奈伤痛
無奈伤痛 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: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

提交回复
热议问题