LINQ to Entities无法识别方法' System.String get_Item(Int32)'方法,并且此方法无法转换为商店表达式

时间:2016-08-12 15:50:08

标签: c# entity-framework linq

当我执行此查询时,我收到此错误:

return (from i in _ctx.TestPackages
   where i.Id==TestPackageId

   select new  ViewTestPackageState
   {
       Id = i.Id,
       PackageNumber = i.PackageNumber,
       ReInstatement = ReturnTestPackageState(i.Id, "Reinstatment")[0],
       ReInstatementReportNumber = ReturnTestPackageState(i.Id, "Reinstatment")[1],
       ReInstatementSubmitDateTime = ReturnTestPackageState(i.Id, "Reinstatment")[2],

       TestReportNumber = ReturnTestPackageState(i.Id, "Test")[1],
       TestState = ReturnTestPackageState(i.Id, "Test")[0],
       TestSubmitDateTime = ReturnTestPackageState(i.Id, "Test")[2],

       CleanReportNumber = ReturnTestPackageState(i.Id, "Clean")[1],
       CleanSubmitDateTime = ReturnTestPackageState(i.Id, "Clean")[2],
       CleaningState = ReturnTestPackageState(i.Id, "Clean")[0],

       Drying = ReturnTestPackageState(i.Id, "Drying")[0],
       DryingReportNumber = ReturnTestPackageState(i.Id, "Drying")[1],
       DryingSubmitDateTime = ReturnTestPackageState(i.Id, "Drying")[2],

       Flushing = ReturnTestPackageState(i.Id, "Flushing")[0],
       FlushingReportNumber = ReturnTestPackageState(i.Id, "Flushing")[1],
       FlushingSubmitDateTime = ReturnTestPackageState(i.Id, "Flushing")[2],

       LineCheckReportNumber = ReturnTestPackageState(i.Id, "LineCheck")[1],
       LineCheckState = ReturnTestPackageState(i.Id, "LineCheck")[0],
       LineCheckSubmitDateTime = ReturnTestPackageState(i.Id, "LineCheck")[2],

       Size = i.Size.ToString(),
       TestPackageOrder = i.TestPackageOrder,

   }).ToList();

错误:LINQ to Entities无法识别方法' System.String get_Item(Int32)'方法,并且此方法无法转换为商店表达式

我知道问题是因为查询而查询应该在select语句之前执行,所以我的问题是如何在select之前执行此查询?

2 个答案:

答案 0 :(得分:2)

执行查询,然后从结果中选择对象

var result = (from i in _ctx.TestPackages
   where i.Id==TestPackageId).ToList();

return(result.Select( new ViewTestPackageState{...}));

答案 1 :(得分:2)

var rawList = (from i in _ctx.TestPackages where i.Id==TestPackageId).ToList();

return rawList.Select(i => new ViewTestPackageState
       {
           Id = i.Id,
           PackageNumber = i.PackageNumber,
           ReInstatement = ReturnTestPackageState(i.Id, "Reinstatment")[0],
           // etc