2010-06-24 70 views

回答

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

也許這就是你需要:

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

因此,首先定義要搜索的事物列表,然後使用它對列表(columnDiffList)進行搜索。

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