Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我有一个只有 6 个属性的自定义类列表。有一个字段叫做“状态”
我将它们放在通用列表中
List<House> hList = GetHomes(); //Holds 204 instances of House int count = hList.Where(x=>x.status == "sold").ToList().Count();
这样做会返回 3,这是正确的,但这样做需要 10 秒。
有更快的方法吗?
如果你只需要计数,你可以这样做:
var count = hList.count(x => x.status == "sold");
这将防止迭代 3 次(对于 where,然后 tolist,然后 count)