2011-05-10 94 views
0

列表幫助我有一個搜索我的LINQ

list<fruitObj> 


    Fruit 
    FruitName, FruitColor 
    banana, yellow 
    orange, orange 
    cherry, red 

    List<FruitObj> test = new List<FruitObj>(); 

有搜索列表與BAN啓動的內聯的方式,如果它不包含字符串剛剛返回true?

+0

您可以發佈FruitObj類的源代碼嗎? – Larry 2011-05-10 20:26:23

回答

4
bool hasBAN = test.Any(x => x.FruitName.StartsWith("BAN")); 

注意,這是區分大小寫,來匹配 「香蕉」 你可以這樣做:

bool hasBAN = test.Any(x => x.FruitName.StartsWith("BAN", StringComparison.InvariantCultureIgnoreCase)); 
3

這確實不區分大小寫

bool hasAnyBAN = test.Any(x=>x.StartsWith("BAN", StringCoparison.InvariantCultureIgnoreCase)); 
0

很多假設這裏..

如果一個FruitObj有一個.Name屬性可以是其中的一個你上市的名字,你可以這樣做:

list.Any(f => f.Name.StartsWith("BAN")); 
0
test.Where(fo => fo.FruitName.ToUpper().StartsWith("BAN")); 

if (test.Count(fo => fo.FruitName.ToUpper().StartsWith("BAN")) > 0) 
{ 
    //... 
} 
3
bool startsWithBan = test.Any(f => 
    f.Name.StartsWith("ban", StringComparison.InvariantCultureIgnoreCase)); 

假設名稱是包含水果名稱的屬性。

0
var hasBAN = test.Any(fruit => fruit.FruitName.ToUpper().StartsWith("BAN"));