locked
Search in LINQ to Entity RRS feed

  • Question

  • Hello,
    I use LINQ in my aplication and I have been big problem Wink I must writing, a search which finds item in many tables. I have a generincs list which have keeps words. I'm finding this words. I don't know how many items I finding. My code:
    Can you help mi ?? Any suggestions??
    List<string>words
    object wyszukaj(BAZA_DANYCH bazadanych, List<string>words)
    {
    var primary = from czlowiek in bazadanych.APPLICANT
    select new
    {
    czlowiek.NAME,
    czlowiek.SURNAME,
    czlowiek.CITY
    };

    foreach(string word in words)
    {
    var slowo = from czlowiek in bazadanych.APPLICANT
    where (czlowiek.NAME.Contains(word) || czlowiek.SURNAME.Contains(word) || czlowiek.CITY.Contains(word))
    select new
    {
    czlowiek.NAME,
    czlowiek.SURNAME,
    czlowiek.CITY
    };
    primary = primary.Intersect(slowo);
    }
    return primary;
    }
     
    Tuesday, January 27, 2009 10:56 PM

Answers

  • Can't use the loop variable (word) like that, because of defered execution and shared scope of variables in closures.

     

    Change to this:

     

    Code Snippet

    foreach(string x in words)

    {

      string word = x;

      var slowo....

     

     

     

    Wednesday, January 28, 2009 4:57 PM