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

2023-09-07 14:43:25 作者:青衫默

下面EX pression返回一个接触 - 几十个属性的整个接触。这是好的,但是,理想情况下,我想回报是联系人的ID(contact.contactId)唯一的财产。如何做到这一点?

  VAR assocOrg = Contacts.Where(X => x.ContactTypeID == 2及和放大器; x.OrganizationName ==XYZ公司);
 

解决方案

  VAR的结果= Contacts.Where(X => ...)
                     。选择(X => x.ContactID);
 

  VAR的结果=从X通讯录
             其中x.ContactTypeID == 2&安培;&安培; x.OrganizationName ==XYZ公司
             选择x.ContactID;
 
linqpad 5 破解版 LINQPadv5.25 中文版 腾牛下载

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");

解决方案

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;