你的位置:首页 > ASP.net教程

[ASP.net教程]LINQ多个操作嵌套实现


获取集合,需要使用多个条件Where,排序OrderBy,查询Select一起。

先来分步实现:


source code:

 string[] stringArray = { "hgdgh", "qerty", "Haktjjy", "zcvz", "rtwrt" };var contains = Enumerable.Where(stringArray, n => n.Contains("t"));var orderby = Enumerable.OrderBy(contains, n => n.Length);var select = Enumerable.Select(orderby, n => n.ToUpper());var query = select;

View Code

 


如果把它们写成一句,就能实现操作嵌套:

 



source code:

IEnumerable<string> q = Enumerable.Select(                  Enumerable.OrderBy(                  Enumerable.Where(new string[] { "hgdgh", "qerty", "Haktjjy", "zcvz", "rtwrt" }, n => n.Contains("t")),                                                          n => n.Length),                                                          n => n.ToUpper());      q.ForEach(delegate (string str) {        Write(str);        WriteLiteral("<br />");      });

View Code

 

太于过复杂,当嵌套很多层时,根本不好看。各人习惯之。