遇到一个问题,在同一个列表,在不同的场景需要显示不同的内容,如果用Dictionary因为只有一对键和键值对,比较麻烦。使用List的Find方法就可以解决这个问题,数据量不大的情况,效率也不会有影响。
static void Main(string[] args)
{
strNum sn = listTest.Find(s => s.d.Equals("俩"));
string lower = sn.b;
string upper = sn.c;
Console.WriteLine(lower + " " + upper);
Console.ReadKey();
}
public class strNum
{
public string b { get; set; }
public string c { get; set; }
public string d { get; set; }
}
public static List<strNum> listTest = new List<strNum>()
{
new strNum(){b="2",c="二",d="俩"},
new strNum(){b="3",c="三",d="仨"}
};
运行结果: