//Lambda表达式详解 //int //List<int> numbers = new List<int> {1,2,3,4,5,6,7,8,9 }; //var n = numbers.Where(a => (a > 3)); //int [] ns = n.ToArray();//返回一个原对象的数组 //string //List<string> strs = new List<string> {"1","2","3"}; //var s = strs.Where(a => (Convert.ToInt16(a)>2)); //string[] ss = s.ToArray(); //object //List<People> strs = new List<People> { new People("evan", 20), new People("evanpei", 25)}; //var s = strs.Where(a => (a.age > 20)); //People[] ss = s.ToArray(); //Linq 列转换为集合 //DataTable dt = DBHelper.GetDataTable("SELECT b.ID,a.MODEL_NAME,b.PLAN_NAME FROM dbo.t_Model AS a LEFT JOIN dbo.t_TestPlan AS b ON b.ID = a.TESTPLAN_ID", 0, null); //List<string> Modes = new List<string>();//所有方案 去重复 //var list = dt.AsEnumerable().Distinct().Select(t => t.Field<string>("PLAN_NAME")).ToList(); //Modes = list;
C#3.0 LINQ 查询语法 首先来看一个很简单的LINQ查询例子,查询一个int数组中小于5的数字,并按照大小顺序排列: static void Main(string[] args) { int[] arr = new int[] { 8, 5, 89, 41, 1, 2, 3, 65, 1 }; var m = from n in arr where n < 5 orderby n descending select n;//小于5,并且倒序排列显示 foreach (var n in m) { Console.WriteLine(n); } Console.ReadLine(); }