搜索列表.Net 3.5,类似于SQL - 列(value1,value2,...,value)

时间:2010-06-24 07:59:18

标签: linq-to-entities

我确实有List<ColumnDiff> columnDiffList

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

我需要确定是否存在columnName为“A”,“B”,“C”的元素 提取子列表并不重要。

在SQL术语中 columName in('A','B','C')

如何在LINQ中编写代码

3 个答案:

答案 0 :(得分:2)

也许这就是你所需要的:

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

首先定义要搜索的内容列表,然后使用它来对列表进行搜索(columnDiffList)。

答案 1 :(得分:1)

有几种方法,这是一个简单的例子:

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

答案 2 :(得分:0)

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