【问题标题】:The LINQ expression node type 'ArrayIndex' is not supported in LINQ to EntitiesLINQ to Entities 不支持 LINQ 表达式节点类型“ArrayIndex”
【发布时间】:2015-08-19 11:38:51
【问题描述】:
 var residenceRep = 
     ctx.ShiftEmployees
        .Include(s => s.UserData.NAME)
        .Include(s => s.ResidenceShift.shiftName)
        .Join(ctx.calc,
              sh => new { sh.empNum, sh.dayDate },
              o => new { empNum = o.emp_num, dayDate = o.trans_date },
              (sh, o) => new { sh, o })
        .Where(s => s.sh.recordId == recordId && s.o.day_flag.Contains("R1"))
        .OrderBy(r => r.sh.dayDate)
        .Select(r => new
             {
                  dayDate = r.sh.dayDate,
                  empNum = r.sh.empNum,
                  empName = r.sh.UserData.NAME,
                  shiftId = r.sh.shiftId,
                  shiftName = r.sh.ResidenceShift.shiftName,
                  recordId,
                  dayState = r.o.day_desc.Split('[', ']')[1]
             }).ToList();

我得到一个例外:

LINQ to 不支持 LINQ 表达式节点类型“ArrayIndex” 实体

我如何在此查询中找到Split('[', ']')[1] 的替代方法

【问题讨论】:

    标签: c# asp.net linq entity-framework linq-to-entities


    【解决方案1】:

    您必须在加载数据后提交查询并进行拆分:

    var residenceRep = 
     ctx.ShiftEmployees
        .Include(s => s.UserData.NAME)
        .Include(s => s.ResidenceShift.shiftName)
        .Join(ctx.calc,
              sh => new { sh.empNum, sh.dayDate },
              o => new { empNum = o.emp_num, dayDate = o.trans_date },
              (sh, o) => new { sh, o })
        .Where(s => s.sh.recordId == recordId && s.o.day_flag.Contains("R1"))
        .OrderBy(r => r.sh.dayDate)
        .Select(r => new
             {
                  dayDate = r.sh.dayDate,
                  empNum = r.sh.empNum,
                  empName = r.sh.UserData.NAME,
                  shiftId = r.sh.shiftId,
                  shiftName = r.sh.ResidenceShift.shiftName,
                  recordId = r.sh.recordId,
                  dayState = r.o.day_desc,
    
             })
        .ToList()//Here we commit the query and load data
        .Select(x=> {
                  var parts = x.dayState.Split('[', ']');
                  return new {
                       x.dayDate,
                       x.empNum,
                       x.empName,
                       x.shiftId,
                       x.shiftName,
                       x.recordId,
                       dayState = parts.Length > 1 ?parts[1]:"",                 
                 };
          })
          .ToList();
    

    【讨论】:

    • Property or indexer 'AnonymousType#1.dayState' cannot be assigned to -- it is read only
    • 我认为最好的做法是使用.AsEnumerable() 而不是.ToList(),因为它不会完全遍历查询结果。见这里:msdn.microsoft.com/en-us/library/vstudio/…
    【解决方案2】:

    我遇到了这个问题,我选择的方法是获取我想要的所有元素并将它们保存List 中,然后过滤其中的实际数据列表。 我知道这不是最佳答案,但它对我有用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-01-22
      • 1970-01-01
      • 2011-08-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多