[英]How can I parse a value in linq to entity?
I want to do something like this: 我想做这样的事情:
var apps = from app in context.Apps
where (platform == AppPlatform.All ||
(app.Platform == sPlatform && new Version(app.PlatformVersion) <= version))&&
(availability == AppAvailability.All || app.Availability == sAvailability)
select app;
return apps.ToList();
The line new Version(app.PlatformVersion) <= version))
is causing an error: Only parameterless constructors and initializers are supported in LINQ to Entities.
new Version(app.PlatformVersion) <= version))
导致错误: Only parameterless constructors and initializers are supported in LINQ to Entities.
Basically what I need is to make my entity model parse the app.PlatformVersion as a new Version() object, instead of string, but I apparently can't do this from within my linq-to-entity. 基本上,我需要使我的实体模型将app.PlatformVersion解析为一个新的Version()对象,而不是字符串,但是显然我不能在linq-to-entity中执行此操作。 Can I do this at the entity-model level?
我可以在实体模型级别执行此操作吗? I have other fields (strings) that I'd like to parse into types as well (like parsing a string to an Enum).
我还想将其他字段(字符串)解析为类型(例如将字符串解析为枚举)。 How would I accomplish this?
我将如何完成?
Because EF tries to translate the Linq query to SQL and there is no way to translate Version method. 由于EF尝试将Linq查询转换为SQL,因此无法转换Version方法。 So you could just query with other conditions first and store it in memory, then query from the in-memory object using the complex Linq query.
因此,您可以先查询其他条件并将其存储在内存中,然后使用复杂的Linq查询从内存中对象进行查询。
Technically you could do this, (you can definitely make it better in terms of the performance) 从技术上讲,您可以做到这一点(就性能而言,您绝对可以使其更好)
var apps_temp = from app in context.Apps.All().ToList();
//this query will not be translated to SQL.
var apps = from app in apps_temp
where (platform == AppPlatform.All ||
(app.Platform == sPlatform && new Version(app.PlatformVersion) <= version))&&
(availability == AppAvailability.All || app.Availability == sAvailability)
select app;
return apps.ToList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.