Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LINQ在使用Regex.Split()时抛出错误_C#_Regex_Linq_Entity Framework_Asp.net Mvc 4 - Fatal编程技术网

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
,然后查看发生了什么。