LINQ to DataSet, distinct by multiple columns

Ganesha picture Ganesha · Apr 1, 2009 · Viewed 34.3k times · Source

Just wanted to check if there is way to do distinct by multiple columns. Thanks in advance!!!

BTW, I found a great LINQ extension here but need some guidance to use it for multiple columns

Answer

Marc Gravell picture Marc Gravell · Apr 1, 2009

Well, you can do the projection first:

var qry = db.Customers.Select(cust => new {cust.ID, cust.Name, cust.Region})
                    .Distinct();

Or in query syntax:

var qry = (from cust in db.Customers
          select new {cust.ID, cust.Name, cust.Region}).Distinct();

That do?