Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ Distinct()

Tags:

I am working on getting the results of this sql query in LINQ

SELECT DISTINCT(Type)
FROM  Product
WHERE categoryID = @catID

this is my repository query:

public IQueryable<ProdInfo> GetProdInfo()
        {

            var data = from u in db.Prod
                       select new ProdInfo
                       {
                           PID = u.PID,
                           CatID = u.CatID,                           
                           LastChanged = u.LastChanged,
                           ChangedBy = u.ChangedBy,                               
                           Type = u.Type,
                       };

            return data;
        }

filter:

public static IQueryable<ProdInfo> GetDistinctProdType(this IQueryable<ProdInfo> qry,int CatID)
            {
                return from p in qry
                       where p.CatID.Equals(CatID)
                       select p;
            }

I need the filter to return the distinct prod type? How can i do this?

like image 684
Pinu Avatar asked Feb 28 '11 16:02

Pinu


People also ask

What does distinct do in LINQ?

C# Linq Distinct() method removes the duplicate elements from a sequence (list) and returns the distinct elements from a single data source. It comes under the Set operators' category in LINQ query operators, and the method works the same way as the DISTINCT directive in Structured Query Language (SQL).

Why distinct is not working in LINQ?

LINQ Distinct is not that smart when it comes to custom objects. All it does is look at your list and see that it has two different objects (it doesn't care that they have the same values for the member fields). One workaround is to implement the IEquatable interface as shown here.

How do I get distinct on a single column in LINQ?

distinct in Linq to get result based on one field of the table (so do not require a whole duplicated records from table). I know writing basic query using distinct as followed: var query = (from r in table1 orderby r. Text select r).


2 Answers

return (from p in qry
       where p.CatId.Equals(CatID)
       select p.Type).Distinct();

This matches what your provided SQL Query should be doing.

like image 21
msarchet Avatar answered Sep 17 '22 15:09

msarchet


Simply like this:

public static IQueryable<ProdType> GetDistinctProdType(
    this IQueryable<ProdInfo> query,
    int categoryId)
{
    return (from p in query
            where p.CatID == categoryId
            select p.Type).Distinct();
}

Note that I've changed the return type - it should match whatever the type of ProdInfo.Type is.

You may find it more readable to use the extension methods for the whole query if the query expression itself is reasonably simple:

public static IQueryable<ProdType> GetDistinctProdType(
    this IQueryable<ProdInfo> query,
    int categoryId)
{
    return query.Where(p => p.CatID == categoryId)
                .Select(p => p.Type)
                .Distinct();
}
like image 92
Jon Skeet Avatar answered Sep 18 '22 15:09

Jon Skeet