使用 LINQ(集合中的集合)简化代码
给定一个如下所示的对象图;
A --> B[] --> B1[]
--> B2[]
--> C[]
我需要检查 B1 和 B2 的成员/属性的某些条件,并确保 b2.Code 出现在 C[] 内的某个位置。如果满足所有条件,我需要使用 C 和 B 数组元素中的变量构造一个新对象。我当前的尝试如下所示,但我想知道是否可以使用 LINQ 使其更加简洁?
A a = GetA();
List<MyTest> tsts = new List<MyTest>();
foreach (B b in a.B)
{
foreach (B1 b1 in b.B1)
{
if (b1.State == ("READY"))
{
foreach (B2 b2 in b.B2)
{
var tst = (from c in a.C
where c.Code == b2.Code && !c.IsRedundant
select new MyTest
{
Code = c.Code,
BVal = b.BVal,
C1Val = c.C1
}).FirstOrDefault();
if (tst != null)
tsts.Add(tst);
break;
}
}
}
}
Given an object graph that looks something like the following;
A --> B[] --> B1[]
--> B2[]
--> C[]
I need to check certain conditions on members/properties of B1 and B2 as well as ensuring that b2.Code appears somewhere within the C[]. If all conditions are satisfied, I then need to construct a new object using variables from elements of the C and B array. My current attempt is shown below, but I am wondering if this could be made more concise with LINQ?
A a = GetA();
List<MyTest> tsts = new List<MyTest>();
foreach (B b in a.B)
{
foreach (B1 b1 in b.B1)
{
if (b1.State == ("READY"))
{
foreach (B2 b2 in b.B2)
{
var tst = (from c in a.C
where c.Code == b2.Code && !c.IsRedundant
select new MyTest
{
Code = c.Code,
BVal = b.BVal,
C1Val = c.C1
}).FirstOrDefault();
if (tst != null)
tsts.Add(tst);
break;
}
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
绝对地。基本上每个新的
foreach
大致相当于一个额外的from
子句:对此的一些注释:
b1
,这似乎很奇怪,但您将为每个处于“就绪”状态的b1
创建一组额外的测试。foreach (B2 b2 ...)
循环中的无条件break
基本上意味着我们只执行一次循环体 - 因此Take(1)
code>Take(1)
调用获得内部查询(通过扩展方法表示)以获取最多一个结果它是其中一些奇怪的现象很可能可以被消除——但尚不清楚您真正想要实现什么,所以我只是尝试让代码尽可能忠实地重现您的原始查询。
Absolutely. Basically each new
foreach
roughly equates to an extrafrom
clause:A few notes on this:
b1
, but you'll create an extra set of tests for eachb1
which is in the "ready" state.break
within theforeach (B2 b2 ...)
loop basically means we only ever execute the loop body once - hence theTake(1)
Take(1)
call to get at most one resultIt's quite possible that some of these oddities can be removed - but it's not clear what you're really trying to achieve, so I've just tried to make the code reproduce your original query as faithfully as possible.