linqdistinctlinq-to-dataset

LINQ to DataSet, distinct by multiple columns


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


Solution

  • 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?