简体   繁体   中英

Is there any way to call a none LINQ method inside the select new part of an IQueryable object in entity framework?

I have this issue when declaring a IQueryable<T> object inside the:

select new { };

part when I want to call a none LINQ method like Tostring() or any other functions that I have as follows:

select new
{
    TFPricep = CurrancyHelper.DecimalToCurrency(TFPrice),
    TFDatep = TFDate.Tostring()
};

I get this famous error message that says:

LINQ to Entities does not recognize the method 'foo'.

So what happens is that I perform a ToList() and get the data from database and after that I will Have to perform a foreach loop on the list and call functions like ToString() and etc on each list record which has performance issues . as far as I have tested there was no such issue in LINQ to SQL but this issue still exists in Entity Framework

I wonder if there is any way to call none LINQ methods when querying and prevent performing such for each loop ?

没有

When I faced this issue I found two possible solutions but finally decided to avoid using anything that Linq to Entities could not translate.

I found two options:

1) If you are using an .edmx file look at this: http://msdn.microsoft.com/en-us/library/vstudio/dd456857(v=vs.110).aspx

2) This other option is a little uglier but does not require an .edmx file:

http://damieng.com/blog/2009/06/24/client-side-properties-and-any-remote-linq-provider https://www.nuget.org/packages/Microsoft.Linq.Translations/ https://github.com/peschuster/PropertyTranslator

One final comment: I never tried these options so I'm not 100% sure that they even work but they seem legit.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM