本文介绍了在Linq查询中替换Foreach的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 我在foreach循环中有一个linq查询,带有一些if子句 我希望你能帮助我如何在查询中编写它们(不使用foreach) 数字是一个大表,我需要将它的每一行分成四个A,B,C,D并在另一个表中搜索它们并找到类似物 ret = new 列表< ReportData>(); foreach ( var item in 数字) { 字符串 A = item.Substring( 0 , 11 ); string B = item.Substring( 14 , 2 ); string C = item.Substring( 19 , 11 ); string D = item.Substring( 33 ); ret1 =(来自 a 报告 其中​​ aA = A&& aB == B && aC == C&& aD == D&& 过滤器。 Type.Contains(aY) 选择 new ReportData { X = aX, Y = aY,}); if (Filter.Ma!= null && Filter。 Ma.Count > 0 ) { ret1 = ret1.Where( z = > Filter.Ma.Contains(z.Ma)); } if (ret1!= null && ret1.ToList ()。计数> 0 ) { ret.AddRange(ret1) ); } } 解决方案 我讨厌linq语法。我使用扩展语法,所以我可以建立查询来优化它们。 无论哪种方式 - 这是我的刺: var ret = Number.Select(item = > new { A = item.Substring( 0 , 11 ), B = item.Substring( 14 , 2 ), C = item.Substring( 19 , 11 ), D = item.Substring( 33 )}) .SelectMany(n = > Report.Where(a = > aA == nA&& aB == nB && aC == nC&& aD == nD&& Filter.Type.Contains(aY)) .Select(a = > new ReportData { X = aX, Y = aY,} ))。其中(nr = > Filter.Ma == null || !Filter.Ma.Any()|| Filter.Ma.Contains(nr.Ma))。ToList(); 我无法真正测试它,因为我不知道报告,过滤器或 ReportData ,真的很像 更新:分为几个阶段: // 必须是var作为类型为匿名 var q1 = Number.Select(item = > new { A = item.Substring( 0 , 11 ), B = item.Substring( 14 , 2 ), C = item.Substring( 19 , 11 ), D = item.Substring( 33 )})。AsQueryable(); // 不是必需的,但应该有助于优化 // 选择很多是非常优秀的。我没有看到你得到更好的回报 var q2 = q1.SelectMany(n = > Report.Where(a = > aA == nA&& aB == nB && aC == nC&& aD == nD&& Filter.Type.Contains(aY)) .Select(a = > new ReportData { X = aX, Y = aY,}))。AsQueryable(); // 如果返回类型与之前的变量相同,则可以重复使用它。 var ret = q2.Where(nr = > Filter.Ma == null ||!Filter.Ma.Any()|| Filter.Ma.Contains(nr.Ma)).ToList (); I have a linq query in a foreach loop with some if clausesI wanted you to help me how can I write them all in a query (not using foreach)Number is a big table that I need to divide each line of it to four A,B,C,D and search them in another table and find the similarsret = new List<ReportData>(); foreach (var item in Number) { string A = item.Substring(0, 11); string B = item.Substring(14, 2); string C = item.Substring(19, 11); string D = item.Substring(33); ret1 = (from a in Report where a.A == A && a.B == B && a.C == C && a.D == D && Filter.Type.Contains(a.Y) select new ReportData { X = a.X, Y = a.Y, }); if (Filter.Ma != null && Filter.Ma.Count > 0) { ret1 = ret1.Where(z => Filter.Ma.Contains(z.Ma)); } if (ret1 != null && ret1.ToList().Count > 0) { ret.AddRange(ret1); }} 解决方案 I hate linq syntax. I use extension syntax so I can build up queries to refine them.Either way - This is my stab:var ret = Number.Select(item => new{ A = item.Substring(0, 11), B = item.Substring(14, 2), C = item.Substring(19, 11), D = item.Substring(33)}).SelectMany(n => Report.Where( a => a.A == n.A && a.B == n.B && a.C == n.C && a.D == n.D && Filter.Type.Contains(a.Y)) .Select(a => new ReportData { X = a.X, Y = a.Y, })).Where(nr => Filter.Ma == null || !Filter.Ma.Any() || Filter.Ma.Contains(nr.Ma)).ToList();I couldn't really test it as I don't know what Report, Filter or ReportData, really look likeUpdate: Broken down into stages://Must be a var as the type is anonymousvar q1 = Number.Select(item => new{ A = item.Substring(0, 11), B = item.Substring(14, 2), C = item.Substring(19, 11), D = item.Substring(33)}).AsQueryable(); //Not a requirement but should help optimize//Select many is pretty darn optimal. I don't see you getting a better return that thisvar q2 = q1.SelectMany(n => Report.Where( a => a.A == n.A && a.B == n.B && a.C == n.C && a.D == n.D && Filter.Type.Contains(a.Y)) .Select(a => new ReportData { X = a.X, Y = a.Y, })).AsQueryable();//If the return type is the same a previous variable then you can reuse it.var ret = q2.Where(nr => Filter.Ma == null || !Filter.Ma.Any() || Filter.Ma.Contains(nr.Ma)).ToList(); 这篇关于在Linq查询中替换Foreach的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-23 13:18