Linq and Contains()
I am using following method:
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 returns the 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?
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
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
精彩评论