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

标签 c# linq .net-4.0

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

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

最佳答案

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;

关于c# - 从 LINQ 查询结果返回单个属性,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4287789/

相关文章:

c# - 关闭 .NET SerialPort 后的 ObjectDisposedExecption

c# - 为什么 GetType() 为 Nullable<DateTime> 返回 DateTime 类型

c# - 如何将 AutoFixture 自定义应用于从基类继承的任何内容?

c# - 将 IAsyncEnumerable 转换为列表

c# - Microsoft Chart Control 中的折线图

c# - 如何在忽略 DateTime 属性的时间部分的同时选择具有 Linq by Date 的项目?

c# - 如何使用 linq 从表中搜索任何属性?

asp.net - ASP.NET 4 中是否可以默认打开 HTML 编码?

c# - XDocument中当前节点的路径

c# - 如何在类内部调用构造函数?