简体   繁体   English

具有明显列表的C#Linq类

[英]C# Linq Class with Distinct List

I have a problem trying to get a Distinct List of my Class Objects. 我在尝试获取我的类对象的不同列表时遇到问题。

For this particular query, I am only interested in grabbing the DLType, There are 3 types of 'DLType' that appear in my List multiple times. 对于此特定查询,我只想获取DLType,我的列表中多次出现3种类型的“ DLType”。 I just want to grab those three types from the data and use it for the datasource for a combo box. 我只想从数据中获取这三种类型,并将其用于组合框的数据源。

Here is My Class :- 这是我的课:-

public class DistributionList
{
    public int DistributionID { get; set; }
    public string DistributionName { get; set; }
    public string  DLType { get; set; }
}

Here is where I grab the data:- 这是我获取数据的地方:

public List<DistributionList> GetDistributionLists()
{
    DataTable dt = new DataTable();

    OleDbCommand cmd = new OleDbCommand("Select * FROM [DistributionLists]",
                                                 Connection);
    Adapter.SelectCommand = cmd;

    Connection.Open();
    Adapter.SelectCommand.ExecuteNonQuery();
    Adapter.Fill(dt);
    Connection.Close();

    var DistributionLists = (from d in dt.AsEnumerable()
             select new DistributionList
             {
                 DistributionID = d.Field<int>("DistributionID"),
                 DistributionName = d.Field<string>("DistributionName"),
                 DLType = d.Field<string>("DLType")
             }).ToList();

    return DistributionLists;

}

Here I pull the data into my Form :- 在这里,我将数据放入表单:

var distributionData = dc.GetDistributionLists();

Now i need to get a distinct list from the distributionData Object:- 现在我需要从distributionData对象中获得一个不同的列表:-

var query = (from d in distributionData
                         select new DistributionList
                         {
                             DLType = d.DLType

                         }).Distinct().ToList();

But this doesn't work, Is there a way of doing this? 但这是行不通的,有办法吗?

The problem with your Linq is that it generates a new DistributionList for each value - and because they're all different instances, Distinct returns them all. Linq的问题在于,它会为每个值生成一个新的DistributionList并且由于它们都是不同的实例,因此Distinct会全部返回它们。 Try reordering the way you process the query, something like this: 尝试重新排序查询的方式,如下所示:

var query = (from d in distributionData select d.DLType)
                  .Distinct()
                  .Select(t => new DistributionList() { DLType = t })
                  .ToList();

You need to implement Equals on your DistributionList class. 您需要在DistributionList类上实现Equals See this answer for details. 有关详细信息,请参见此答案

不要选择new DistributionList ,请选择DLType

distributionData.Select(d => d.DLType).Distinct().ToList();

you just need to group list by some unique key and after select the first element from each group, something like this 您只需要按一些唯一的键对列表进行分组,然后从每个组中选择第一个元素,就可以像这样

(from d in distributionData
                         select new DistributionList
                         {
                             DLType = d.DLType

                         }).GroupBy(a=>a.DLType).Select(a=>a.First()).ToList()

You could use a Lambda Comparer for the Distinct Method: 您可以对不同的方法使用Lambda比较器:

public class LambdaComparer<T> : IEqualityComparer<T>
{
    private readonly Func<T, T, bool> _lambdaComparer;
    private readonly Func<T, int> _lambdaHash;

    public LambdaComparer(Func<T, T, bool> lambdaComparer) :
        this(lambdaComparer, o => 0)
    {
    }

    public LambdaComparer(Func<T, T, bool> lambdaComparer, Func<T, int> lambdaHash)
    {
        if (lambdaComparer == null)
            throw new ArgumentNullException("lambdaComparer");
        if (lambdaHash == null)
            throw new ArgumentNullException("lambdaHash");

        _lambdaComparer = lambdaComparer;
        _lambdaHash = lambdaHash;
    }

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

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

and then use it like follow: 然后像下面这样使用它:

var query = (from d in distributionData
             select d)
             .Distinct<DistributionList>
             (new LambdaComparer<DistributionList>((x, y) 
                                 => x.DLType == y.DLType));

Greetings 问候

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM