简体   繁体   English

使用Linq从2个不同的对象中进行选择

[英]Selecting with linq from 2 different objects

i want to select from 2 different objects in Linq in order to compare them. 我想从Linq中的2个不同对象中进行选择,以便进行比较。 This is what i tried, 这就是我尝试过的

var myItem = (from abc in firstList.Value
              from cds in secondList
              where (abc.Key.theKey == cds.secondList.theSecondKey
              select cds).SingleOrDefault();

although i get an error: 虽然我得到一个错误:

Type inference failed in the call to 'SelectMany' 调用“ SelectMany”时类型推断失败

If that's your exact query, it may just be because you've got unmatched brackets. 如果这是您的确切查询,则可能只是因为您有无与伦比的括号。 Try this: 尝试这个:

var myItem = (from abc in firstList.Value
              from cds in secondList
              where abc.Key.theKey == cds.secondList.theSecondKey
              select cds).SingleOrDefault();

Admittedly I would probably rewrite that using a join - in most cases the join will be more efficient. 诚然,我可能会使用联接重写它-在大多数情况下,联接会更高效。

However, if that's not your exact query, please post a short but complete program which demonstrates the problem. 但是,如果这不是您的确切查询,请发布一个简短但完整的程序来演示问题。 It's not clear why cds would have a secondList property for example. 目前尚不清楚为什么cds具有例如secondList属性。 A complete example demonstrating the problem would make this a lot simpler. 一个完整的例子来说明这个问题会使事情变得简单得多。

You have an opening paranthesis in more: 您在以下方面有一个开放的幻想:

var myItem = (from abc in firstList.Value
              from cds in secondList
              where abc.Key.theKey == cds.secondList.theSecondKey
              select cds
             ).SingleOrDefault();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM