Linq Distinct() by name for populate a dropdown list with name and value(Linq Distinct() 按名称填充带有名称和值的下拉列表)
问题描述
我正在尝试使用制药公司(例如 Bayer、Medley 等)填充下拉列表.而且,我从 DB 获取这些名称,并且这些名称在 DB 中重复,但 ID 不同.
I'm trying to populate a Drop down list with pharmaceutical companies, like Bayer, Medley etc. And, I'm getting theses names from DB and theses names are repeated in DB, but with different id's.
我正在尝试使用 Linq Distinct(),但我不想使用相等比较器.还有别的办法吗?
I'm trying to use Linq Distinct(), but I don't want to use the equality comparer. Is there another way?
我的下拉列表必须填写id和公司名称.
My drop down list must be filled with the id and the name of the company.
我正在尝试类似的东西:
I'm trying something like:
var x = _partnerService
.SelectPartners()
.Select(c => new {codPartner = c.codPartner, name = c.name})
.Distinct();
这是在 ddl 中显示重复的公司.
This is showing repeated companies in ddl.
谢谢!
推荐答案
以下表达式将仅选择不同的公司并返回第一个出现的公司及其 ID.
The following expression will select only distinct companies and return the first occurence with its id.
partnerService.SelectPartners().GroupBy(p => p.Name).Select(g => g.First());
这篇关于Linq Distinct() 按名称填充带有名称和值的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:Linq Distinct() 按名称填充带有名称和值的下拉列表


基础教程推荐
- C# - 将浮点数转换为整数...并根据余数更改整数 2022-01-01
- 从 VB6 迁移到 .NET/.NET Core 的最佳策略或工具 2022-01-01
- Page.OnAppearing 中的 Xamarin.Forms Page.DisplayAlert 2022-01-01
- 覆盖 Json.Net 中的默认原始类型处理 2022-01-01
- 如何使用OpenXML SDK将Excel转换为CSV? 2022-01-01
- 当键值未知时反序列化 JSON 2022-01-01
- 我什么时候应该使用 GC.SuppressFinalize()? 2022-01-01
- 创建属性设置器委托 2022-01-01
- 使用 SED 在 XML 标签之间提取值 2022-01-01
- C# - 如何列出发布到 ASPX 页面的变量名称和值 2022-01-01