LINQ在使用Regex.Split()时抛出错误

时间:2014-06-04 04:41:17

标签: 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.

FOR CODE

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();

注意

field_value的错误点位于List<DropDownValue>

中的FormDetailsModel类型

x.field_valueString我正在使用String[]将其转换为Regex.Split(),然后转换为List<DropDownValue>以在field_value上转让它

分割后如何从field_value分配x.field_value

1 个答案:

答案 0 :(得分:1)

您必须替换

 select i).AsEnumerable().Select(x=> new FormDetailsModel()

 select i).ToList().Select(x=> new FormDetailsModel()

使用.ToList()时,将查询数据库并选择扩展程序在本地运行。否则lambda表达式被编译成sql查询,显然SQL并不知道Regex.Split()。在Linq2Objects中,您可以使用该方法,因为它在本地运行。重要的是要记住您正在使用的LinqTo *。