如何最好地组合这两个 LINQ 表达式?

发布于 2024-10-30 06:50:02 字数 374 浏览 1 评论 0原文

第一个表达式使用另一个标识值检索联系人的 ID。第二个表达式使用联系人 ID 检索整个联系人。看起来我应该能够将这两个陈述合并为一个,但我太挣扎了(累、压力大、犯愚蠢的错误等)。这两个语句有效,我得到了我需要的结果,但我觉得它可以更清晰,并且可能是单个表达式。

感谢大家的帮助!

var contactId = DAL.dc.ContactMrns.Where(cm => cm.MRN == recipient.MRN)
.Select(x => x.ContactId)
.First();

var contact = DAL.dc.Contacts.Where(c => c.ContactID == contactId).First();

The first expression retrieves a contact's ID using another identifying value. The second expression retrieves the whole contact using a contact ID. It seems like I should be able to merge these two statements into one but I'm struggling too much (tired, stressed, making dumb mistakes, etc.). The two statements work and I get the result I need but I feel it could be cleaner and probably a single expression.

Thanks for everyone's help!

var contactId = DAL.dc.ContactMrns.Where(cm => cm.MRN == recipient.MRN)
.Select(x => x.ContactId)
.First();

var contact = DAL.dc.Contacts.Where(c => c.ContactID == contactId).First();

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

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

发布评论

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

评论(4

哭泣的笑容 2024-11-06 06:50:02

好吧,看起来这可能是一个连接:

var contact = (from cm in DAL.dc.ContactMrns
               where cm.MRN == recipient.MRN
               join c in DAL.dc.Contacts on c.ContactID equals cm.ContactID
               select c).First();

请注意,您可能想要使用 Single() 而不是 First() 来明确表示您确实只期望一个单一结果。

另一种选择是使用采用表达式的单个重载

var contact = DAL.dc.Contacts.Single
   (c => c.ContactID == DAL.dc.ContactMrns.Single
         (cm => cm.MRN == recipient.MRN).ContactID);

Well, it looks like that's probably a join:

var contact = (from cm in DAL.dc.ContactMrns
               where cm.MRN == recipient.MRN
               join c in DAL.dc.Contacts on c.ContactID equals cm.ContactID
               select c).First();

Note that you might want to use Single() instead of First() to make it clear that you really do only expect a single result.

Another option is to use the Single overload which takes an expression:

var contact = DAL.dc.Contacts.Single
   (c => c.ContactID == DAL.dc.ContactMrns.Single
         (cm => cm.MRN == recipient.MRN).ContactID);
£冰雨忧蓝° 2024-11-06 06:50:02

直接使用第二个表达式。这样,您就拥有了 Contact 对象,并且直接拥有了 contact 和 contactId

Use directly second expression. BY that, you have Contact object, And by that both contact and contactId directly

初心 2024-11-06 06:50:02

你可以这样做:

var contact = DAL.dc.Contacts.First(
    c => c.ContactID == DAL.dc.ContactMrns.First(
        cm => cm.MRN == recipient.MRN));

You could do:

var contact = DAL.dc.Contacts.First(
    c => c.ContactID == DAL.dc.ContactMrns.First(
        cm => cm.MRN == recipient.MRN));
面犯桃花 2024-11-06 06:50:02

当然可以,只是有没有用的问题。

var contact = DAL.dc.Contacts.First(contact => 
       contact.ContactId == DAL.dc.ContactMrns.First(mrns =>
             mrns.MRN == recipient.MRN))

Sure, you can, it's just a question of whether or not it would be useful to.

var contact = DAL.dc.Contacts.First(contact => 
       contact.ContactId == DAL.dc.ContactMrns.First(mrns =>
             mrns.MRN == recipient.MRN))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文