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;
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 />"); });
太于过复杂,当嵌套很多层时,根本不好看。各人习惯之。