I have the following LINQ statement (stationId is an int and version is a byte array):
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 run 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])' method, and this method cannot be translated into a store expression.
After a bit of reading I am aware that the problem is LINQ cannot convert the SequenceEqual method call into a SQL equivalent (I think anyway). Does anyone know of a workaround solution for this? Or perhaps could point me in the right direction when trying to use byte arrays with LINQ queries, I couldn't find an existing question specifically to do with byte arrays.
Thanks in advance.
EDIT: Using Jani's post this was the final code used to resolve 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();
You are correct in the way you interpret the error, but you don't need SequenceEquals()
at all - you can directly compare the byte array provided you bind to varbinary(MAX)
in the DB.
var foundStation = (from wd in _Context.AssignmentWizardDatas
from station in wd.AssignmentWizardStationDatas
where station.Id == stationId
where station.Version == version
select station).SingleOrDefault();
//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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With