@manhnguyenv

Welcome to my blog!

LINQ Distinct with IQueryable

December 5, 2017 17:30

LINQ Distinct with IQueryable

Solution

var resultAsQueryable = myList.GroupBy(test => test.id).Select(grp => grp.First());

var result = myList.GroupBy(test => test.id).Select(grp => grp.First()).ToList();

Output

[{id=1,value="a"},{ id = 2, value = "c" }]

Input

List<LinqTest> myList = new List<LinqTest>();
myList.Add(new LinqTest() { id = 1, value = "a" });
myList.Add(new LinqTest() { id = 1, value = "b" });
myList.Add(new LinqTest() { id = 2, value = "c" });

Returning a Distinct IQueryable with LINQ?

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;
}
 

Categories

Recent posts