Linq array

Last post 09-15-2008 4:04 PM by atmonline. 2 replies.

Sort Posts:

  • Linq array

    09-15-2008, 3:21 PM
    • Member
      56 point Member
    • atmonline
    • Member since 06-14-2006, 2:37 PM
    • Posts 30

    hi friends,

    How to write linq to find out supplied set of values exists in an array. eg:-

    string[] letters = { "A", "B", "C", "D", "E", "F", "G" };
    bool IsMatch =
        (from n in letters where new string[] { "B", "X" }
             .Contains(n) select n)
             .Count() == new string[] { "B", "X" }.Count();

    the above code return false and its work as expected, but i am looking for a better code.

    thanks,
    achu.

  • Re: Linq array

    09-15-2008, 3:43 PM
    Answer

    achu,

    I've pulled out the second array for clarity.  I think your code is fine, but you could use the Intersect extension if you wanted:

    string[] letters = { "A", "B", "C", "D", "E", "F", "G" };

    string[] test = { "B", "X" };

    bool isMatch =

    (from n in letters select n).Intersect(from t in test select t).Count() == test.Count();

    James

    James Ashley, Magenic Technologies
    (james.ashley.magenic@gmail.com)
  • Re: Linq array

    09-15-2008, 4:04 PM
    • Member
      56 point Member
    • atmonline
    • Member since 06-14-2006, 2:37 PM
    • Posts 30

    thanks james.

Page 1 of 1 (3 items)