LINQ to Entities Error Using SequenceEqual Enumerated Method

I have the following LINQ statement (stationId is int, and version is an array of bytes):

var foundStation = (from wd in _Context.AssignmentWizardDatas from station in wd.AssignmentWizardStationDatas where station.Id == stationId where station.Version.SequenceEqual(version) select station).SingleOrDefault(); 

When the above code is executed, I encounter the following error:

LINQ to Entities does not recognize the method 'Boolean SequenceEqual [Byte] (System.Collections.Generic.IEnumerable 1[System.Byte], System.Collections.Generic.IEnumerable 1 [System.Byte])', and this method cannot be translated into the storage expression.

After a little reading, I know that the problem is that LINQ cannot convert the call to the SequenceEqual method to the SQL equivalent (I think, anyway). Does anyone know of a workaround for this? Or perhaps I could point me in the right direction when trying to use byte arrays with LINQ queries, I could not find an existing question specifically regarding byte arrays.

Thanks in advance.

EDIT . Using the Jani message, this was the last code used to fix this error:

  //eager execution of the linq query to find the stations var foundStation = (from wd in _Context.AssignmentWizardDatas from station in wd.AssignmentWizardStationDatas where station.Id == stationId select station).ToList(); //finding the result in memory var result = (from f in foundStation where f.Version.SequenceEqual(version) select f).SingleOrDefault(); 
+4
source share
2 answers
 //eager execution of the linq query to find the stations var foundStation = (from wd in _Context.AssignmentWizardDatas where wd.Id == stationId select station).ToList(); //finding the result in memory var result = from f in foundStation where f.version.SequenceEqual(version) select f; 
+4
source

You are correct in how you interpret the error, but you do not need SequenceEquals() at all - you can directly compare an array of bytes if you are attached to varbinary(MAX) in the database.

 var foundStation = (from wd in _Context.AssignmentWizardDatas from station in wd.AssignmentWizardStationDatas where station.Id == stationId where station.Version == version select station).SingleOrDefault(); 
+8
source

Source: https://habr.com/ru/post/1347539/


All Articles