LINQ to Entities不支持LINQ表达式节点类型'ArrayIndex'

时间:2015-08-19 11:38:52

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

 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]的替代

2 个答案:

答案 0 :(得分: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();

答案 1 :(得分:0)

我有这个问题,我选择的方法是将我想要的所有元素和保存它们放入列表然后过滤实际的数据名单。 我知道这不是最好的答案,但它对我有用。