使用LinQ -C#选择帮助。选择两个对象 [英] Help in Select with LinQ -C#. Select with two Objects
本文介绍了使用LinQ -C#选择帮助。选择两个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,我有这些对象。
For example I have these objects.
<object1>
<list1>
<info>
<codedetails>A</code1>
</info>
<info>
<codedetails>B</code1>
</info>
</list1>
</object1>
<object2>
<lst>
<reference>1</reference>
<info>
<code1>X</code1>
</info>
<info>
<code1>D</code1>
</info>
</lst>
<lst>
<reference>2</reference>
<info>
<code1>A</code1>
</info>
<info>
<code1>A</code1>
</info>
</lst>
<lst>
<reference>3</reference>
<info>
<code1>A</code1>
</info>
<info>
<code1>B</code1>
</info>
</lst>
</object2>
在这种情况下,我需要获得参考3,因为两个code1同意(A - > ; A,B-> B)。
我尝试
In this case I need get the reference 3 because the two code1 agree ( A -> A, B-> B).
I try
var result = (from p in object1.list1
from f in object2.lst
where f.info.code1 == p.info.code1
select f.reference).FirstOrDefault();
最后一个代码的结果是引用2,但它将是引用3.
我可以使用LINQ引用3的结果吗?
在这种情况下我有两个代码(A和B)进行比较,但我会有更多。
非常感谢。
The result of last code is the reference 2 but it will be the reference 3.
Can I the result of the reference 3 with LINQ?.
In this case I have two code ( A and B ) to compare, but I will have more.
Thank you very much.
推荐答案
您使用的是FirstOrDefault ()仅获得第一个结果。这就是为什么它只提供参考2而不是其他。
如果你只需要第一个结果然后使用
you are using FirstOrDefault() to get the first result only. That's why it's giving only reference 2 not other.
if you need first result only then use
var result = (from p in object1.list1
from f in object2.lst
where f.info.code1 == p.info.code1
select f.reference).FirstOrDefault();
如果你需要两个结果然后使用
If you need both Result then use
var result = (from p in object1.list1
from f in object2.lst
where f.info.code1 == p.info.code1
select f.reference).ToList();
如果你只需要最后的结果那么使用
If you need last result only then use
var result = (from p in object1.list1
from f in object2.lst
where f.info.code1 == p.info.code1
select f.reference).LastOrDefault();
这篇关于使用LinQ -C#选择帮助。选择两个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文