Search code examples
c#asp.netlinqentity-frameworklinq-to-entities

The LINQ expression node type 'ArrayIndex' is not supported in 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();

I get an exception :

The LINQ expression node type 'ArrayIndex' is not supported in LINQ to Entities

How i could find an alternative to Split('[', ']')[1] in this query


Solution

  • You must commit the query and do the split after loading the data:

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