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

 

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

 

posted @ 2016-04-12 14:38  Insus.NET  阅读(1997)  评论(0编辑  收藏  举报