Distinct on Multiple Columns Entity Framework LINQ

c# entity-framework linq linq-to-entities

Question

What is the LINQ Equivalent of

Select DISTINCT A, B, C from TESTDB WHERE ALPHA =1

I am trying something like this:

var data = TESTDB.WHERE(i=>i.ALPHA==1).SELECT(A,B,C).DISTINCT();
1
17
4/8/2015 12:25:11 AM

Accepted Answer

Using anonymous objects will do the trick:

var data = TESTDB.Where(i => i.ALPHA == 1).Select(i => new {i.A, i.B, i.C}).Distinct();
21
8/6/2012 10:32:45 PM

Popular Answer

You can also try

db.Table
  .OrderBy(m=>m.Name)
  .DistinctBy(m=> new{m.SerialNumber, m.Manufacturer})
  .ToList();


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow