Search code examples
linqlinq-to-entities

LINQ to Entities does not recognize the method 'Char get_Chars(Int32)' method, and this method cannot be translated into a store expression


I would like to select only the records that have the field "1" from the string eventTriggers (that looks something like this : "00100010" )

I've tried and succesfully done so with more than 1 calls .. but i doubt its efficient. Basically I would want something like this ... but apprently LINQ does not support this.
(LINQ to Entities does not recognize the method 'Char get_Chars(Int32)' method, and this method cannot be translated into a store expression.)

     using (var service = new dB.Business.Service.BaseBusinessService<memo>())
   {
      List<memo> result = service.Repository.GetQuery().Where(p => p.ID == ID && p.eventTriggers[index] == '1').ToList();
   }

Any hints towards the correct solution ? Thank you !


Solution

  • EF can't convert the char array operation into a valid query. How about

    IEnumerable<Memo> memos
    using (var service = new dB.Business.Service.BaseBusinessService<Memo>())
    {
        memos = service.Repository.GetQuery()
                        .Where(p => p.ID == ID).AsEnumerable();
    }
    
    var result = memos.Where(m => m.eventTriggers[index] == '1').ToList();
    

    This gets all the memos with a matching ID locally then filters on the eventTriggers array.


    Alternatively you could convert eventTriggers into a numeric value and use a bit mask, this would probably be a much faster query.

    Linq looking like this,

    using (var service = new dB.Business.Service.BaseBusinessService<Memo>())
    {
        result = service.Repository.GetQuery()
                     .Where(p => 
                             p.ID == ID
                         &&
                             m.eventTriggers & mask != 0).ToList();
    }
    

    more exapmles here