一、对象数组查询
1—简单型
Product[] products = new Product[]
{
new Product{Id=1,Name="Tomato Soup",Category="Groceries",Price=1},
new Product{Id=2,Name="Yo-yo",Category="Toys",Price=3.75M},
new Product{Id=3,Name="Hammer",Category="Hardware",Price=16.99M}
};
//主语句
var product = products.FirstOrDefault((p) => p.Id == id);
if(product==null) return NotFound();
2—另一种语法
List<userModel> list = new List<userModel>();
list.Add(new userModel()
{
Name="张三",
Sex="男",
Address="黑龙江省长春市道外区",
Age=41,
});
list.Add(new userModel()
{
Name="陈四省",
Sex="男",
Address="江西省九江市",
Age=65,
});
list.Add(new userModel()
{
Name = "吴老狗",
Sex = "男",
Address = "浙江省苏州市",
Age = 77,
});
//相等时,条件为t.Name.trim()=="张三"
var dbRes = from t in list where t.Name.Contains("省") select t;
if (dbRes != null && dbRes.Count() > 0)
{
Console.WriteLine("搜索: sex=" + dbRes.FirstOrDefault().Sex + " 地址=" + dbRes.FirstOrDefault().Address);
}