Linq Distinct() 按名称填充带有名称和值的下拉列表
- 作者: 那晚越女说我?
- 来源: 51数据库
- 2022-12-08
问题描述
我正在尝试使用制药公司(例如 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());
- C#通过fleck实现wss协议的WebSocket多人Web实时聊天(附源码)
- 团队城市未满足要求:MSBuildTools12.0_x86_Path 存在
- 使用 MSBuild.exe 在发布模式下构建 C# 解决方案
- 当我发布 Web 应用程序时,AfterPublish 脚本不运行
- 构建时 T4 转换的产品仅在下一个构建中使用
- ASP.NET Core Application (.NET Framework) for Windows x64 only error in project.assets.json
- 新的 .csproj 格式 - 如何将整个目录指定为“链接文件"到子目录?
- 如何将条件编译符号(DefineConstants)传递给 msbuild
- MSBuild 支持 Visual Studio 2017 RTM 中的 T4 模板
- NuGet 包还原找不到包,没有源
