locked
Have to filter that contains "Ian" "Mac" and "Lynn" (LINQ) RRS feed

  • Question

  • Hi to all,

    I'm new to LINQ and I have a little problem.

    In my LINQ statement I have to filter that contains some string in my data collection.

    Lets say I have to filter the name that contains "Ian" "Mac" and "Lynn"

     

    Example Data:

    Name               Address

    Mac Ian          Pasay City

    Michael Angelo  Florida

    Lynn Ian           Manila

    Lynn Andre       Quezon

     

     

    Here's my LINQ

     var p = from a in ad.myResult()
                        where a.name.Contains("a")
                        select a;


    3 stars and a sun
    Tuesday, April 5, 2011 4:57 AM

Answers

  • If you're filtering by "Ian" "Mac" AND "Lynn" (your string must have all three components), here's the code:

     var p = from a in ad.myResult()
              where a.name.Contains("Ian")
              && a.name.Contains("Mac")
              && a.name.Contains("Lynn")
              select a;
    
    // This also works:
     var p = from a in ad.myResult()
              where a.name.Contains("Ian")
              where a.name.Contains("Mac")
              where a.name.Contains("Lynn")
              select a;
    • Proposed as answer by L.J Lincoln Wednesday, April 13, 2011 11:58 AM
    • Marked as answer by Jackie-Sun Monday, April 25, 2011 3:40 AM
    Tuesday, April 5, 2011 10:52 AM

All replies

  • If you're filtering by "Ian" "Mac" AND "Lynn" (your string must have all three components), here's the code:

     var p = from a in ad.myResult()
              where a.name.Contains("Ian")
              && a.name.Contains("Mac")
              && a.name.Contains("Lynn")
              select a;
    
    // This also works:
     var p = from a in ad.myResult()
              where a.name.Contains("Ian")
              where a.name.Contains("Mac")
              where a.name.Contains("Lynn")
              select a;
    • Proposed as answer by L.J Lincoln Wednesday, April 13, 2011 11:58 AM
    • Marked as answer by Jackie-Sun Monday, April 25, 2011 3:40 AM
    Tuesday, April 5, 2011 10:52 AM
  • Hi,

    Is it possible to have a one where statement which the same as the above code...

     

     

    thanks


    3 stars and a sun
    Wednesday, April 6, 2011 1:44 AM
  • Hi pinoyz, what do you mean of one where statement which the same as the above code...?

    Is JRS52's reply your answer? I think it is!

    Thanks,

    Wednesday, April 13, 2011 11:58 AM