获取列表的项目数.使用Linq [英] Get item count of a list<> using Linq

查看:84
本文介绍了获取列表的项目数.使用Linq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想查询一个列表<>,并找出许多项目如何符合选择标准.使用LINQ和c#/.net 3.5.我将如何修改查询以返回int计数.

I want to query a List<> and find out how MANY items match the selection criteria. using LINQ and c# /.net 3.5. How would I modify the query to return an int count.

var specialBook = from n in StoreDisplayTypeList 
                  where n.DisplayType=="Special Book" 
                  select n;

推荐答案

 var numSpecialBooks = StoreDisplayTypeList.Count(n => n.DisplayType == "Special Book");

这使用了 Enumerable.Count 的重载,该重载需要一个Func<TSource, bool>谓词可过滤序列.

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

这篇关于获取列表的项目数.使用Linq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆