-1

I have one table with name CateringPurchaseOrder. That table contains one column with name "PONO". that column contains same vlaue more than once. I want to retrieve record based on the unique value in "pono" column.

Example:

pono  column1
1        value1 
1        value2  
2        value3  
2        value4  

output:

pono    coulm1 
1           value1  
2           value3  

I have tried with the following code:

IEnumerable<CateringPurchaseOrder> order =   entites.CateringPurchaseOrders.Distinct<CateringPurchaseOrder>(new CateringPorderComparer());

public class CateringPorderComparer : IEqualityComparer<CateringPurchaseOrder>
{
    bool IEqualityComparer<CateringPurchaseOrder>.Equals(CateringPurchaseOrder p1, CateringPurchaseOrder p2)
    {
        return p1.P_O_No == p2.P_O_No;
    }

    int IEqualityComparer<CateringPurchaseOrder>.GetHashCode(CateringPurchaseOrder obj)
    {
        return obj.P_O_No.GetHashCode();
    }
}

I got the following error:

LINQ to Entities does not recognize the method 'System.Linq.IQueryable`1[MilitarymessModel.CateringPurchaseOrder] Distinct[CateringPurchaseOrder](System.Linq.IQueryable`1[MilitarymessModel.CateringPurchaseOrder], System.Collections.Generic.IEqualityComparer`1[MilitarymessModel.CateringPurchaseOrder])' method, and this method cannot be translated into a store expression.

Do you have solution for this problem?

Mat
  • 202,337
  • 40
  • 393
  • 406
giri-webdev
  • 525
  • 10
  • 20

1 Answers1

4

Linq to entities isn't able to parse your CateringPorderComparer into SQL.

If it is not a problem you could do the Distinct operation in your C# code, rather than having the database do it. Depending on how many rows and how many dupoicates there are, this may not be a problem for you.

To do the Distinct after the database query, convert it to a Eumerable before you do the distinct:

entites.CateringPurchaseOrders.AsEnumerable().Distinct<CateringPurchaseOrder>(
new CateringPorderComparer());
Olav Nybø
  • 11,454
  • 8
  • 42
  • 34
  • hi, thanks when i bind the result of this query to the gridview i got the error of "Data source does not support server side data paging". do you have any idea about this problem. – giri-webdev Apr 16 '12 at 03:22
  • check this: http://stackoverflow.com/questions/1661292/the-data-source-does-not-support-server-side-data-paging – Olav Nybø Apr 16 '12 at 08:35
  • The AsEnumerable() call was the magic that fixed my error. Would have been nice to have an overload that took a function, like Where() does, but I'll take what I can get. – Suncat2000 Aug 23 '12 at 19:52