简体   繁体   English

如何将linq中的值解析为实体?

[英]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.

相关问题 我如何通过变量的值访问LINQ(不反映LINQ-to-Entity使用) - How can I access a LINQ by a variable's value (without reflection for LINQ-to-Entity use) 如何使用Linq to XML解析XML字符串并找到以指定字母开头的值的所有节点? - How can I use Linq to XML to parse an XML string and locate all nodes with a value that starts with a specified letter? 如何在Linq中使用Entity Framework编写列表类型返回值? - How can I write list type returning value in Linq with Entity Framework? 如何使用 Linq 将 XML 文档解析为字符串列表 - How can I parse an XML doc into a String List using Linq 如何使用Linq获取唯一的实体集? - How can I grab a unique entity set using Linq? 如何在Linq中更新与数据库断开连接的实体? - How can I update in Linq an entity that is disconnected from database? 如果由包括的实体订购,如何在Linq Select Distinct中订购? - How can I order in Linq Select Distinct if ordered by an Included entity? 实体框架 - 如何在缩写的EF linq中实现此SQL - entity framework - how can I implement this SQL in the abbreviated EF linq 如何加快实体框架LINQ查询的速度? - How can I speed up my Entity Framework LINQ Query? 如何使用不在上下文中的 linq-query 获取实体? - How can I get an entity with linq-query that is not in context?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM