Get item count of a list<> using Linq

▼魔方 西西 提交于 2019-11-29 05:38:58
 var numSpecialBooks = StoreDisplayTypeList.Count(n => n.DisplayType == "Special Book");

This uses an overload of Enumerable.Count that takes aFunc<TSource, bool>predicate to filter the sequence.

Try this:

int specialBookCount = (from n in StoreDisplayTypeList 
                        where n.DisplayType=="Special Book" 
                        select n).Count()

But if you need data as well, you might want to operate with IEnumerable. So, you can use your query and access Count() extension method whenever you want.

var specialBook = from n in StoreDisplayTypeList 
                  where n.DisplayType=="Special Book" 
                  select n;
int num = specialBook.Count();
Édgar Sánchez Gordón

Just surround your query like this: (from ... select n).Count().

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!