开发者

Search in a list .Net 3.5 which analagous to SQL - column in ( value1, value2, ... , valuen )

开发者 https://www.devze.com 2023-01-04 19:06 出处:网络
I do haveList&l开发者_如何学Ct;ColumnDiff> columnDiffList of public class ColumnDiff { public string columnName;

I do have List&l开发者_如何学Ct;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


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).


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;
    }
}


var res = from c in columnDiffList where c.columnName == "A" || c.columnName == "B" select c;
0

精彩评论

暂无评论...
验证码 换一张
取 消