views:

64

answers:

2
+1  Q: 

linq and contains

i have func

public PageOfList<ConsaltQuestion> Filter(int? type, int pageId, EntityCollection<ConsaltCost> ConsaltRoles)
    {
       // return _dataContext.ConsaltQuestion.Where((o => o.Type == type || type == null) && (o=>o.Paid == paid));
        return (from i in _dataContext.ConsaltQuestion where ((i.Type == type || type == null) && (i.Paid == true) && (ConsaltRoles.Contains(ConsaltCostDetails(i.Type.Value)))) select i).ToList().ToPageOfList(pageId, 20);
    }

it return error

LINQ to Entities does not recognize the method 'Boolean Contains(mrhome.Models.ConsaltCost)' method, and this method cannot be translated into a store expression.

how can i fix it?

+2  A: 

Linq to Entities doesn't support the Contains method. In this case you must consider use the Contains filter logic using the in-memory objects (Linq-to-Objects). If it is not a practicable option due to performance reasons, I suggest you to create a stored procedure that performs the contains and then map it to your entity model.

The following url shows the supported query operators http://msdn.microsoft.com/en-us/library/bb738550.aspx

Carlos Loth
A: 

Version 1 of the Entity Framework doesn't support Contains. Version 4 does, but if upgrading isn't feasible you can duplicate it by building up expression tree.

There is a good article here with some example code: http://blogs.msdn.com/alexj/archive/2009/03/26/tip-8-writing-where-in-style-queries-using-linq-to-entities.aspx

Mant101