Search code examples
c#linq-to-entitiesnotsupportedexception

C# LINQ to Entities Select String confusion NotSupportedException


Hi I have this LINQ and in my DB there is a row ind kalk table with MedID="---" and KalkID="00057" and if i hard code them in everything works but if i get them from my stVar which is a String[] i get a NotSupportedException:

ObjectSet<Kalk> kalks = ke.Kalks;  
            var query =  
               from kalk in kalks  
               where kalk.MedID.Equals(stVar[0])//"---"  
                  && kalk.KalkID.Equals(stVar[1])//"00057"  
               select new  
               {  
                   MedID = kalk.MedID,  
                   KalkID = kalk.KalkID,  
                   Navn = kalk.Navn,  
                   ValutaID = kalk.ValutaID,  
                   Sprog = kalk.Sprog,  
                   Dato = kalk.Dato,  
                   SidstRettet = kalk.SidstRettet,  
                   SidstRettetAf = kalk.SidstRettetAf,  
                   Afrunding = kalk.Afrunding,  
                   Kurs = kalk.Kurs,  
                   id = kalk.id  
               };  
            foreach (var item in query)  
            {  
                MessageBox.Show(item.MedID + item.Navn);  
            }

Solution

  • Try this:

    ObjectSet<Kalk> kalks = ke.Kalks;
    
    // Linq-to-SQL (or EF or whichever LINQ provider you use) probably doesn't
    // support array indexers, so put your strings in variables beforehand
    var medID = stVar[0];
    var kalkID = stVar[1];
    
    var query =
        from kalk in kalks
        where kalk.MedID.Equals(medID) //"---"
           && kalk.KalkID.Equals(kalkID) //"00057"
        select new
        {
            MedID = kalk.MedID,
            KalkID = kalk.KalkID,
            Navn = kalk.Navn,
            ValutaID = kalk.ValutaID,
            Sprog = kalk.Sprog,
            Dato = kalk.Dato,
            SidstRettet = kalk.SidstRettet,
            SidstRettetAf = kalk.SidstRettetAf,
            Afrunding = kalk.Afrunding,
            Kurs = kalk.Kurs,
            id = kalk.id
        };
     foreach (var item in query)
     {
         MessageBox.Show(item.MedID + item.Navn);
     }