Search code examples
linqc#-4.0linq-to-entities

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

Solution

  • //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;