从 LINQ 查询结果返回单个属性

发布于 2024-10-04 05:33:32 字数 226 浏览 0 评论 0原文

以下表达式返回一个联系人 - 具有数十个属性的整个联系人。这很好,但理想情况下,我希望返回的只是联系人的 id (contact.contactId) 属性。我该怎么做?

var assocOrg = Contacts.Where(x => x.ContactTypeID == 2 && x.OrganizationName == "COMPANY XYZ");

The following expression returns a contact - the whole contact with dozens of properties. This is fine but, ideally, I'd like the return to be the contact's id (contact.contactId) property only. How do I do this?

var assocOrg = Contacts.Where(x => x.ContactTypeID == 2 && x.OrganizationName == "COMPANY XYZ");

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

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

发布评论

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

评论(5

梦初启 2024-10-11 05:33:33
var result = Contacts.Where(x => ...)
                     .Select(x => x.ContactID);

或者

var result = from x in Contacts
             where x.ContactTypeID == 2 && x.OrganizationName == "COMPANY XYZ"
             select x.ContactID;
var result = Contacts.Where(x => ...)
                     .Select(x => x.ContactID);

or

var result = from x in Contacts
             where x.ContactTypeID == 2 && x.OrganizationName == "COMPANY XYZ"
             select x.ContactID;
假情假意假温柔 2024-10-11 05:33:33

如果您想获得符合您条件的单个或第一个对象,请使用以下命令:

  var result = Contacts.Where(x => ...)
   .Select(x => x.ContactID).FirstOrDefault();

If you want to get a single or first object matching your conditions , use this :

  var result = Contacts.Where(x => ...)
   .Select(x => x.ContactID).FirstOrDefault();
潜移默化 2024-10-11 05:33:33
var assocOrg = Contacts.Where(x => x.ContactTypeID == 2 && x.OrganizationName == "COMPANY XYZ").Select(x=> x.contactId).FirstOrDefault();

这将为您提供第一个 ContactId,接下来将为您提供

var assocOrg = Contacts.Where(x => x.ContactTypeID == 2 && x.OrganizationName == "COMPANY XYZ").Select(x=> x.contactId);

Sql 样式的 ContactId 列表,该列表为

var assocOrg = from contact in Contacts
               where contact.ContactTypeId == 2 && contact.OrganizationName == "COMPANY XYZ"
               select contact.ContactId;
var assocOrg = Contacts.Where(x => x.ContactTypeID == 2 && x.OrganizationName == "COMPANY XYZ").Select(x=> x.contactId).FirstOrDefault();

That would get you the first ContactId and the following would get you a list of ContactId's

var assocOrg = Contacts.Where(x => x.ContactTypeID == 2 && x.OrganizationName == "COMPANY XYZ").Select(x=> x.contactId);

In Sql style that would be

var assocOrg = from contact in Contacts
               where contact.ContactTypeId == 2 && contact.OrganizationName == "COMPANY XYZ"
               select contact.ContactId;
你げ笑在眉眼 2024-10-11 05:33:33
var result = Contacts.Where(x => ...)
           .Select(x => x.ContactID).FirstOrDefault();
var result = Contacts.Where(x => ...)
           .Select(x => x.ContactID).FirstOrDefault();
萌酱 2024-10-11 05:33:33
var assocOrg = Contacts.
               Where(x => x.ContactTypeID == 2 && x.OrganizationName == "COMPANY XYZ").
               Select(x => x.contactId);
var assocOrg = Contacts.
               Where(x => x.ContactTypeID == 2 && x.OrganizationName == "COMPANY XYZ").
               Select(x => x.contactId);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文