-3

I would like to use string array in linq query with IN CLAUSE like below

    string[] strArray = {"A", "B", "C", "D"};
    foreach (var item in SomeCollection.Where(x => x.column.Contains(seriesIIType)))
    {

    }

But I am getting below error.

Error 1 The best overloaded method match for 'string.Contains(string)' has some invalid arguments Error 2 Argument 1: cannot convert from 'string[]' to 'string'

Oluwafemi
  • 14,243
  • 11
  • 43
  • 59
LifeOfPi
  • 625
  • 5
  • 19

3 Answers3

3

It seems to me like you want to do this instead:

SomeCollection.Where(x => strArray.Any(y => x.column.Contains(y)));

But I don't know how x.column.Contains works because I don't know what x.column is.

rory.ap
  • 34,009
  • 10
  • 83
  • 174
2

Call the Contains method on your array.

string[] strArray = {"A", "B", "C", "D"};
SomeCollection.Where(x => strArray.Contains(x.column))

This will return those items which has column property value is either one of those in the array.

Assuming column property is of same type as your array (string)

Shyju
  • 214,206
  • 104
  • 411
  • 497
  • 1
    What if x.column = "ABC"? – Steve Aug 19 '16 at 15:36
  • Then it is not going to return that item. I don't think that is what the OP wanted. My answer was to fix the compiler error where he was not using the method properly. – Shyju Aug 19 '16 at 15:37
  • @Steve I don't get your question still :) – Shyju Aug 19 '16 at 15:39
  • 1
    @Shyju, x.column is type of string. And yes my compile time error has been fixed. – LifeOfPi Aug 19 '16 at 15:42
  • I was pointing to the fact that your answer is _An element of Array Contains Column_, while I had the impression that user wanted _a Column Contains an element of Array_ but he has already made the choice between your answer an the one from rory.ap, so question closed. – Steve Aug 19 '16 at 16:13
  • I see. Thanks for clarifying. – Shyju Aug 19 '16 at 16:14
0

I think it should be the other way around:

string[] seriesIIType = {"OC", "RH", "RC", "OA"};
foreach (var item in SomeCollection.Where(x => seriesIIType.Contains(x.Column)))
        {
        }

For reference: Linq to Entities - SQL "IN" clause

Is it very intuitive, isn't? but that's how it works in Linq world.

Community
  • 1
  • 1