这里有一些受 Reed 启发的工作代码。
您可以将其弹出到 LINQPad 中并查看它的运行情况。通过http://linqpad.com 获取 LINQPad,它会有所帮助!
static bool CheckIT(Dictionary<int, List<TypeA>> theList, TypeA what)
{
return theList.Any(dctnry => dctnry.Value.Any(lst => lst == what));
}
public static void Main()
{
Dictionary<int, List<int>> dict = new Dictionary<int, List<int>>();
dict.Add(1, new List<TypeA>{TypeA.1, TypeA.2, TypeA.3};
dict.Add(11, new List<TypeA>{TypeA.2, TypeA.6, TypeA.7};
dict.Add(23, new List<TypeA>{TypeA.3, TypeA.4, TypeA.9};
if (CheckIT(dict,TypeA.3 ))
Console.WriteLine("Found");
else
Console.WriteLine("Lost");
}
您也可以更进一步,制作一个通用版本,例如
static bool CheckIT<T>(Dictionary<int, List<T>> theList, T what) where T : IEquatable<T>
{
return theList.Any(dict => dict.Value.Any(l => l.Equals(what)));
}
那你会说
if (CheckIT<TypeA>(dict,TypeA.3 ))
但你也可以说
if (CheckIT<int>(dict,13 ))
因为我没有定义 TypeA。