先看一下linq如何过滤数据。类似sql语句,是sql语句的倒装 from where select 。where 也可以写多个。
//利用linq筛选数据 string[] strs = new String[] { "One", "Two", "Three" }; var query = from ss in strs where ss.StartsWith("T") select ss;//查询开头是t的 foreach (String s in query) { Console.WriteLine(s);//输出 two three } Console.ReadLine();
//创建实体类 public class Programer { public String Name { get; set; } public String Language { get; set; } } Programer[] ps = new Programer[] { new Programer(){ Name="张?三y", Language="C#"}, new Programer(){ Name="李?四?", Language="C#"}, new Programer(){ Name="王?五?", Language="Java"} }; //筛选Language==c# 和name==张?三y 的数据 var queryPs = from p in ps where p.Language == "C#" where p.Name == "张?三y" select new { Name = p.Name, Language = p.Language }; //foreach 循环输出 foreach (var p in queryPs) { Console.WriteLine(String.Format("Name:{0}\t\tLanguage:{1}", p.Name, p.Language)); } Console.ReadLine();