Returning a Distinct IQueryable with LINQ?

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

提交回复
热议问题