Ehsan Akbar Ehsan Akbar - 3 months ago 16
C# Question

LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression

When i execute this query i get this error :

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


The error :LINQ to Entities does not recognize the method 'System.String get_Item(Int32)' method, and this method cannot be translated into a store expression

I know the problem is because of the query and the query should be executed before select statement ,so my question is how can i execute this query before select ?

M B M B
Answer

Execute the query and then select object from the result

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

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