【发布时间】:2015-04-04 16:03:44
【问题描述】:
我有 2 个 Linq 查询结果,我想做一些字符串操作和连接。
结果 1 是 group1 中启用的复选框的名称,由
获得var selectedCarPosts = grpBox1MCar.Controls.OfType<CheckBox>()
.Where(c => c.Checked).OrderBy(c => c.Name).Select(c => c.Name);
产生结果 1:
NearMainGate
NearMP5WestGate
结果2是group2中启用的复选框的名称,由
var selectedDtTypeCars = gbDataTypeMCars.Controls.OfType<CheckBox>()
.Where(c => c.Checked).OrderBy(c => c.Name).Select(c => c.Name);
产生 Result2:
WindDir
WindVel
希望从这两个结果中得到一个串联列表,如下所示:(Result3)
C.NearMainGate
C.NearMP5WestGate
C.WindDirNearMainGate
C.WindDirNearMP5WestGate
C.WindVelNearMainGate
C.WindVelNearMP5WestGate
这些在以后的动态 sql 查询中形成列。
我有下面的代码来一步一步完成这个:
var s1 = selectedCarPosts.Select(s => "C." + s); //the first 2 items in Result3
//Now to get the rest, by inserting Result2 string in each of the first 2 items of Result3
IEnumerable<string> selCarPostsArrWithC = new string[]{};
IEnumerable<string> s2 = new string[]{};
foreach (var type in selectedDtTypeCars)
{
selCarPostsArrWithC = s1.Select(s => s.Insert(2, type));//C.WindDirNearMainGate C.WindDirNearMP5WestGate in FIRST iteration and so on
s2 = s2.Concat(selCarPostsArrWithC);// as soon as the SECOND iteration starts, the previous s2 list is overwritten with the subsequent result in selCarPostsArrWithC
}
这里的问题是,在代码调试过程中,我注意到,当我在 foreach 行之后点击 F10 键时,在实际到达 foreach 块之前,s2 中的先前值是已经被 selCarPostsArrWithC 中的后续结果覆盖。下面解释
对于第一次迭代,s2 有结果。
[0] "C.WindDirNearMainGate"
[1] "C.WindDirNearMP5WestGate"
在进入foreach块之前的第二次迭代开始
s2 已经通过 WindVel 重置为新值:
[0] "C.WindVelNearMainGate"
[1] "C.WindVelNearMP5WestGate"
请任何人帮助我做错了什么?对于 IEnumerable 列表,如何以粗体完成 Result3?
【问题讨论】: