LINQ鲜明()按名称填充名称和值的下拉列表 [英] Linq Distinct() by name for populate a dropdown list with name and value

查看:156
本文介绍了LINQ鲜明()按名称填充名称和值的下拉列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图填充一个下拉列表与制药公司,像拜耳,混合泳等。并且,我越来越从数据库论文的名称和论文名称重复的数据块,但使用不同的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鲜明的(),但我不希望使用相等比较。有另一种方式?

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.

我想是这样的:

var x = _partnerService
           .SelectPartners()
           .Select(c => new {codPartner = c.codPartner, name = c.name})
           .Distinct();

这正显示出反复公司DDL。

This is showing repeated companies in ddl.

谢谢!

推荐答案

下面前pression将只选择不同的公司和它的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鲜明()按名称填充名称和值的下拉列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆