How to insert a method inside a LINQ lambda extension method

做~自己de王妃 提交于 2019-12-07 02:28:29

It throws an exception because it tries to convert your code to SQL (and it can't do it). Read this article for some workarounds: http://thoai-nguyen.blogspot.it/2011/05/custom-function-entity-framework.html

If your are working IQueryable (which I believe that you do) then a call to custom .NET method cannot be translated into the query - that's the reason you get exception.

The work-around would be to fetch all possible shopping carts then apply your method on in-memory product list - for example,

searchShoppingCart = shoppingCart.ToList().Where(m => productName.Equals(_ProductRepository.GetProductName(m.Id)))

Of course, this sucks as it defeats the whole purpose!

Better work-around would be to expose product navigable property within shopping cart object and use the same - for example,

searchShoppingCart = shoppingCart.Where(m => productName.Equals(m.Product.Name))
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!