none
"Contains" usage with entity framework models RRS feed

  • Question

  • Hey,

    Lets assume that I have a list of string items. By using the entity framework models I need to check if an ID column of row in a view contains one of the items in the list. Basically something similar like in normal sql "WHERE name IN ('a' , 'b'). As the list can have X number of items, I'm curious how seasoned programmers implement this.

    So something similar like the following:

    var xyz = from x in EntityName.ViewName where x.Id.Contains(/* string item comparison */) select x
    

    Thanks

    Thursday, September 8, 2011 8:27 AM

Answers

  • Hi,

    I don't know if I understand tou at all, but are you talking about something like this:

    var xyz = from x in EntityName.ViewName where list.Contains(x.Id.ToString()) select x
    
    

    Regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    • Marked as answer by Jeremias K Monday, September 12, 2011 8:34 AM
    Thursday, September 8, 2011 9:09 AM

All replies

  • Hi,

    I don't know if I understand tou at all, but are you talking about something like this:

    var xyz = from x in EntityName.ViewName where list.Contains(x.Id.ToString()) select x
    
    

    Regards,

    JA Reyes.


    Please remember to Vote & "Mark As Answer" if this post is helpful to you.
    Por favor, recuerda Votar y "Marcar como respuesta" si la solución de esta pregunta te ha sido útil.
    • Marked as answer by Jeremias K Monday, September 12, 2011 8:34 AM
    Thursday, September 8, 2011 9:09 AM
  • Hi,

    Maybe I was a bit unclear. I ment to ask that what is the easiest way to use List<string> items in the comparison, instead of a normal variable.

    public void DoSmething (List<string>items)
    {
    
    // using entities 
    
    var xyz =(from x in EntityName.ViewName where x.Id.Contains(/* compare list items here */) select x
    
    
    }

     

    Thursday, September 8, 2011 9:20 AM
  • Hi,

    Have you tried what JA Reyes suggested ?

    x.Id is the value you want to search. Contains is not a method of this value but a method of your List<string>. So it will be :

    where items.Contains(x.Id)

    This key is that you have to switch your mind and use what is usual in your programming language (that uses the "list contains value" approach) and forget about SQL (that uses the "value in list" approach).

     

     


    Please always mark whatever response solved your issue so that the thread is properly marked as "Answered".
    Friday, September 9, 2011 5:57 PM
  • Hi,

    Yeah it was my mind which was slowing me down. This is what too much traditional SQL causes.

    Thanks for both of you.

     

    Monday, September 12, 2011 8:36 AM