我想查询List <>并找出MANY项如何与选择条件匹配.使用LINQ和c#/.net 3.5.如何修改查询以返回int计数.
var specialBook = from n in StoreDisplayTypeList where n.DisplayType=="Special Book" select n;
Ani.. 43
var numSpecialBooks = StoreDisplayTypeList.Count(n => n.DisplayType == "Special Book");
这使用了一个Enumerable.Count
带Func
谓词来过滤序列的重载.
var numSpecialBooks = StoreDisplayTypeList.Count(n => n.DisplayType == "Special Book");
这使用了一个Enumerable.Count
带Func
谓词来过滤序列的重载.
试试这个:
int specialBookCount = (from n in StoreDisplayTypeList where n.DisplayType=="Special Book" select n).Count()
但是,如果您还需要数据,则可能需要使用IEnumerable进行操作.因此,您可以随时使用查询并访问Count()扩展方法.
var specialBook = from n in StoreDisplayTypeList where n.DisplayType=="Special Book" select n; int num = specialBook.Count();
只需将您的查询包围如下:(from ... select n).Count()
.