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

发布于 2024-10-02 10:41:27 字数 310 浏览 3 评论 0原文

我想从 Linq 中的 2 个不同对象中进行选择以进行比较。这是我尝试过的,

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

尽管我收到错误:

在调用“SelectMany”时类型推断失败

i want to select from 2 different objects in Linq in order to compare them. 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'

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

小情绪 2024-10-09 10:41:27

如果这是您的精确查询,可能只是因为您的括号不匹配。试试这个:

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

诚然,我可能会使用连接重写它 - 在大多数情况下连接会更有效。

但是,如果这不是您的确切查询,请发布一个简短但完整的程序来演示该问题。例如,尚不清楚为什么 cds 会有一个 secondList 属性。演示该问题的完整示例将使事情变得简单得多。

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. A complete example demonstrating the problem would make this a lot simpler.

游魂 2024-10-09 10:41:27

more 中有一个左括号:

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

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();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文