Checking if a table contains values in a list

后端 未结 4 755
被撕碎了的回忆
被撕碎了的回忆 2021-01-21 23:26

I have a parameter list of strings and I want to write a query that returns a list of strings that contain values of the parameter list that are present in the table. I have the

4条回答
  •  遇见更好的自我
    2021-01-21 23:30

    Based on Contain's Documentation the parameter for Contains could be string not a list of string. So it should be like this:

    List TheListParameter = new List {"string1", "string2", "string3"};
    
    var TheOutput = (from t in MyDC.SomeTable
                     where TheListParameter.Contains(t.SomeColumn.ToString())
                     select t.SomeColumn).ToList();
    

    Or if you want to return a boolean result if MyDC.SomeTable.SomeColumn contains one of the TheListParameter's strings you can try something like this:

    bool hasSameElements = MyDC.SomeTable.ToList()
                          .Select(c => c.SomeColumn)
                          .Intersect(TheListParameter).Any(); //True or False
    

提交回复
热议问题