views:

15

answers:

3

I do have List<ColumnDiff> columnDiffList

of

  public class ColumnDiff
    {
        public string columnName;
        public string leftValue;
        public string rightValue;
    }

I need to determine whether there are elements where columnName either "A", "B" , "C" It is not essential to extract a subList.

In SQL terms
columName in ( 'A' , 'B' , 'C' )

How to code that in LINQ

+2  A: 

Maybe this is what you need:

var searchList = new[] {"A", "B", "C"};
var result = columnDiffList.Where(i => searchList.Any(j => j == i.columnName));

So first define the list of things you want to search for and then use it to do the search against your list (columnDiffList).

Padel
A: 
var res = from c in columnDiffList where c.columnName == "A" || c.columnName == "B" select c;
Orsol
+1  A: 

There are a few ways, here is a simple example:

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<ColumnDiff> columnDiffs = new List<ColumnDiff>();
            columnDiffs.AddRange(new[]  {
                                             new ColumnDiff(){columnName="Aa"}
                                            ,new ColumnDiff(){columnName="A"}
                                            ,new ColumnDiff(){columnName="B"}
                                            ,new ColumnDiff(){columnName="Bb"}
                                            ,new ColumnDiff(){columnName="C"}
                                            ,new ColumnDiff(){columnName="Cc"}
                                        });

            bool hasItems = columnDiffs.Exists(x => x.columnName == "A" || x.columnName == "B" || x.columnName == "C");
            hasItems = columnDiffs.Any(x => x.columnName == "A" || x.columnName == "B" || x.columnName == "C");
            hasItems = columnDiffs.FirstOrDefault(x => x.columnName == "A" || x.columnName == "B" || x.columnName == "C") != null;

            Console.ReadKey();
        }
    }

    public class ColumnDiff
    {
        public string columnName;
        public string leftValue;
        public string rightValue;
    }
}
slugster