具有独特列表的C#Linq类

时间:2012-08-22 11:51:50

标签: c# linq distinct

我在尝试获取我的类对象的不同列表时遇到问题。

对于这个特定的查询,我只对抓取DLType感兴趣,有多种类型的“DLType”多次出现在我的列表中。我只想从数据中获取这三种类型,并将其用作组合框的数据源。

这是我的班级: -

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

这是我抓取数据的地方: -

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;

}

在这里,我将数据拉入我的表格: -

var distributionData = dc.GetDistributionLists();

现在我需要从distributionData对象中获取一个不同的列表: -

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

                         }).Distinct().ToList();

但这不起作用,有没有办法做到这一点?

5 个答案:

答案 0 :(得分:1)

Linq的问题在于它为每个值生成一个新的DistributionList - 并且因为它们都是不同的实例,Distinct将全部返回。尝试重新排序处理查询的方式,如下所示:

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

答案 1 :(得分:1)

您需要在Equals课程上实施DistributionList。有关详细信息,请参阅this answer

答案 2 :(得分:1)

不要选择new DistributionList,请选择DLType

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

答案 3 :(得分:1)

你只需要通过一些唯一的密钥对列表进行分组,然后从每个组中选择第一个元素,就像这样

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

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

答案 4 :(得分:0)

您可以将Lambda Comparer用于Distinct方法:

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);
    }
}

然后使用它如下:

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

问候