C# LINQ在使用Regex.Split()时抛出错误
错误C# LINQ在使用Regex.Split()时抛出错误,c#,regex,linq,entity-framework,asp.net-mvc-4,C#,Regex,Linq,Entity Framework,Asp.net Mvc 4,错误 LINQ to Entities does not recognize the method 'System.String[] Split(System.String, System.String)' method, and this method cannot be translated into a store expression. 用于代码 var getfrmbid = (from e in _dbEntity.FormNames where e.form_i
LINQ to Entities does not recognize the method 'System.String[] Split(System.String, System.String)' method, and this method cannot be translated into a store expression.
用于代码
var getfrmbid = (from e in _dbEntity.FormNames
where e.form_id == id & e.type == "Form"
select new FormsCreationModel
{
form_name = e.form_name,
form_id = e.form_id,
formfields = (from i in _dbEntity.FormDetails
where e.form_id == i.form_id
select i).AsEnumerable().Select(x=> new FormDetailsModel()
{
field_default = x.field_default,
field_id = x.field_id,
field_mandatory = x.field_mandatory,
field_maxlength = x.field_maxlength,
field_name = x.field_name,
field_type = x.field_type,
field_validation = x.field_validation,
field_value = Regex.Split(x.field_value, " ^ ").Select(item => new DropDownValue() { DDValue = item }).ToList()
}).ToList()
}).Single();
注意
var getfrmbid = (from e in _dbEntity.FormNames
where e.form_id == id & e.type == "Form"
select new FormsCreationModel
{
form_name = e.form_name,
form_id = e.form_id,
formfields = (from i in _dbEntity.FormDetails
where e.form_id == i.form_id
select i).AsEnumerable().Select(x=> new FormDetailsModel()
{
field_default = x.field_default,
field_id = x.field_id,
field_mandatory = x.field_mandatory,
field_maxlength = x.field_maxlength,
field_name = x.field_name,
field_type = x.field_type,
field_validation = x.field_validation,
field_value = Regex.Split(x.field_value, " ^ ").Select(item => new DropDownValue() { DDValue = item }).ToList()
}).ToList()
}).Single();
字段值处的错误点
属于FormDetailsModel
x.field\u值
是一个字符串
,我正在使用Regex.Split()
将其转换为String[]
,然后将其分配到列表
拆分后,如何从x.field\u值
分配field\u值?您必须替换
select i).AsEnumerable().Select(x=> new FormDetailsModel()
与
当您使用.ToList()时,将查询DB并选择本地运行的扩展。否则lambda表达式将编译成sql查询,显然sql不知道Regex.Split()。在LINQ2对象中,您可以使用该方法,因为它在本地运行。记住你在用什么是很重要的 LINQtoEntities将查询转换为SQL,以便在数据库上运行它。无法将正则表达式转换为正确的SQL。您正在执行FormNames
和FormDetails
之间的联接,并且只将FormDetails
转换为可枚举项。这意味着查询的某些部分仍在数据库中,无法使用正则表达式。我可以理解这种情况。但除了“我做了什么”到“我能做什么”之外,还有其他建议吗?如何将字段值
分配给x.field\u值
,然后在查询遍历表单字段
集合并使用正则表达式更新字段值
字段之后?如果单独运行填充表单字段
属性的查询,会发生什么情况?AsEnumerable
应该可以避免此问题。同时尝试将此AsEnumerable
更改为ToList
,然后查看发生了什么。