見以下示例
using?System;using?System.Collections.Generic;
using?System.Linq;
namespace?MyApplication
{
class?Program
{
public?class?MyClass
{
public?string?Name?{?get;?set;?}
public?bool?IsTrue?{?get;?set;?}
}
static?void?Main(string[]?args)
{
Dictionary<int,?MyClass>?dic?=
new?Dictionary<int,?MyClass>();
//添加
dic.Add(1,?new?MyClass?{?Name?=?"張三",?IsTrue?=?false?});
dic.Add(2,?new?MyClass?{?Name?=?"張四",?IsTrue?=?true?});
dic.Add(3,?new?MyClass?{?Name?=?"張五",?IsTrue?=?false?});
dic.Add(4,?new?MyClass?{?Name?=?"張六",?IsTrue?=?true?});
dic.Add(5,?new?MyClass?{?Name?=?"張七",?IsTrue?=?false?});
int?c1?=?dic.Count(e?=>?e.Value.IsTrue?==?true);
Console.WriteLine(c1);?//輸出2
int?c2?=?dic.Count(e?=>?e.Value.IsTrue?==?false);
Console.WriteLine(c2);?//輸出3
//?或者
var?qry1?=?from?x?in?dic
where?x.Value.IsTrue?==?true select?x;int?c3?=?qry1.Count();
Console.WriteLine(c3);?//輸出2
var?qry2?=?from?x?in?dic
where?x.Value.IsTrue?==?false select?x;int?c4?=?qry2.Count();//輸出3
Console.WriteLine(c4);
}
}
}