C# Linq Distinct() 按名称填充带有名称和值的下拉列表

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/912188/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 02:38:53  来源:igfitidea点击:

Linq Distinct() by name for populate a dropdown list with name and value

c#linq-to-sqldistinct

提问by AndreMiranda

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.

我正在尝试使用制药公司(例如 Bayer、Medley 等)填充下拉列表。而且,我从 DB 获取这些名称,并且这些名称在 DB 中重复,但 ID 不同。

I'm trying to use Linq Distinct(), but I don't want to use the equality comparer. Is there another way?

我正在尝试使用 Linq Distinct(),但我不想使用相等比较器。还有其他方法吗?

My drop down list must be filled with the id and the name of the company.

我的下拉列表必须填写公司的 id 和名称。

I'm trying something like:

我正在尝试类似的东西:

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

This is showing repeated companies in ddl.

这显示了 ddl 中的重复公司。

thanks!

谢谢!

采纳答案by Daniel Brückner

The following expression will select only distinct companies and return the first occurence with its id.

以下表达式将仅选择不同的公司并返回第一个出现的公司及其 ID。

partnerService.SelectPartners().GroupBy(p => p.Name).Select(g => g.First());

回答by Robert Harvey

Distinct works on the entire select. If you include c.codPartner in the select, and there are two different values of c.codPartner for the same c.name, then you are going to see two rows with the same c.name.

Distinct 适用于整个选择。如果在选择中包含 c.codPartner,并且同一个 c.name 有两个不同的 c.codPartner 值,那么您将看到具有相同 c.name 的两行。

回答by Scott Ivey

just pass in your own comparer to the Distinct method using one of the other overloads.

只需使用其他重载之一将您自己的比较器传递给 Distinct 方法。

(extension) IQueryable<T> IQueryable<T>.Distinct( IEqualityComparer<T> comparer )

回答by James Curran

If you do not specify a IEqualityComparer parameter, then it will just use Object.ReferenceEquals, which looks at the objects GetHashKey value. For anonymous types, they are unique.

如果您没有指定 IEqualityComparer 参数,那么它将只使用 Object.ReferenceEquals,它查看对象的 GetHashKey 值。对于匿名类型,它们是唯一的。

Now solving this is a bit tricky, since you cannot write an IEqualityComparer for an anonymous type. So you muct create a real type for the problem:

现在解决这个问题有点棘手,因为您不能为匿名类型编写 IEqualityComparer。所以你必须为这个问题创建一个真正的类型:

class Partner
{
    public int codPartner {get; set;}
    public string name {get; set;}
    public override int GetHashCode() { return name .GetHashCode();}
}

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

回答by wegrata

I don't think you can do this with an anonymous class, but if you created a data object like

我不认为你可以用匿名类来做到这一点,但是如果你创建了一个像

    class Foo
{
    private int _ID;

    public int ID
    {
        get { return _ID; }
        set { _ID = value; }
    }
    private string _Name;

    public string Name
    {
        get { return _Name; }
        set { _Name = value; }
    }

}

you could create a comparer object like

您可以创建一个比较器对象,如

    class FooComparer : IEqualityComparer<Foo>
{


    public bool Equals(Foo x, Foo y)
    {
        return x.Name == y.Name;
    }

    public int GetHashCode(Foo obj)
    {
        return obj.GetHashCode();
    }

}

回答by Amy B

var distinctCompanies = Companies
  .GroupBy(c => c.CompanyName)
  .Select(g => g.First());

回答by Johan Leino

Distinc will use GetHashCode if you don′t tell it (via an IEqualityComparer) to use another method. You could use a generic equalitycomparer, like this:

如果您不告诉它(通过 IEqualityComparer)使用另一种方法,则 Distinc 将使用 GetHashCode。您可以使用通用的等式比较器,如下所示:

public class GenericEqualityComparer<T> : IEqualityComparer<T>
{    
    private Func<T, T, Boolean> comparer;    

    public GenericEqualityComparer(Func<T, T, Boolean> comparer)    
    {        
        this.comparer = comparer;    
    }    

    #region IEqualityComparer<T> Implementation

    public bool Equals(T x, T y)    
    {        
        return comparer(x, y);    
    }    

    public int GetHashCode(T obj)    
    {
        return obj.GetHashCode();  
    }    

    #endregion
}

and then use like this (kindof)

然后像这样使用(有点)

public static IEqualityComparer<YourType> MyComparer
{
   get
     {
      return new GenericEqualityComparer<YourType>((x, y) =>
       {
          return x.name.Equals(y.name);
        });
      }
}