Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LINQ to Entities error using SequenceEqual enumerable method

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.IEnumerable1[System.Byte], System.Collections.Generic.IEnumerable1[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();
like image 725
MattStacey Avatar asked Dec 13 '22 14:12

MattStacey


2 Answers

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();
like image 113
BrokenGlass Avatar answered Jan 26 '23 07:01

BrokenGlass


//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; 
like image 23
Jahan Zinedine Avatar answered Jan 26 '23 08:01

Jahan Zinedine