繁体   English   中英

LINQ获取不同的值并填写LIST

[英]LINQ Get Distinct values and fill LIST

我试图找出是否可以使用LINQ向我提供我在DataTable(FirstName,LastName,QTY)中的一些数据的不同值。 我可以获得不同的值并填充我的列表,但我必须运行两个不同的LINQ查询来获取它....我相信有更好的方法来做到这一点:)

任何建议将不胜感激(LINQ非常新)

码:

public static List<StudentData> LinqDistinct(DataTable dt)
{
      DataTable linqTable = dt;

       //get the distinct values
        var query =
            (from names in dt.AsEnumerable()
             select new {
                 FirstName = names.Field<string>("FirstName"),
                 LastName = names.Field<string>("LastName")
             }).Distinct();


        //fill my list with the distinct values
        List<StudentData> sList = (from sa in query.AsEnumerable()
                                   select new StudentData
                                              {
                                                  FirstName = sa.FirstName,
                                                  LastName = sa.LastName
                                                  //Qty = names.Field<int>("Qty")

                                                 }).ToList();                                               



        return sList;}

任何理由都不要简单地在不同之后进行投影,你可能需要在Distinct之后的AsEnumerable(),但这不是什么大不了的事。

public static List<StudentData> LinqDistinct(DataTable dt)
{
    DataTable linqTable = dt;
    return
        (from names in dt.AsEnumerable()
         select new {
             FirstName = names.Field<string>("FirstName"),
             LastName = names.Field<string>("LastName")
         }).Distinct().Select(x =>
             new StudentData() { FirstName=x.FirstName, LastName=x.LastName})
             .ToList();
}

从您的问题中不清楚数量是DataTable中的值,还是给定项目的重复数量,如Ecyrb的答案。 但是,如果您StudentData类实现IEquatable<StudentData>或覆盖Equals方法,这应该工作:

public static List<StudentData> LinqDistinct(DataTable dt)
{
    return dt.AsEnumerable()
             .Select(row => new StudentData
                            {
                                FirstName = row.Field<string>("FirstName"),
                                LastName = row.Field<string>("LastName"),
                                Qty = row.Field<int>("Qty")
                            })
             .Distinct()
             .ToList();
}

如果StudentData不支持值比较,并且您无法将该支持添加到类中,则可能必须创建IEqualityComparer<StudentData>并将其传递给Distinct方法。

很确定你可以这样做......

List<StudentData> sList = (from names in dt.AsEnumerable().Distinct() // I am not sure if you even have to call AsEnumerable()
                 select new StudentData() {
                     FirstName = names.Field<string>("FirstName"),
                     LastName = names.Field<string>("LastName")
                 }).ToList();

你可以使用GroupBy

public static List<StudentData> LinqDistinct(DataTable dt)
{
    List<StudentData> results = dt.AsEnumerable()
        .GroupBy(row => 
        {
            FirstName = row.Field<string>("FirstName"),
            LastName = row.Field<string>("LastName")
        })
        .Select(group => new StudentData
        {
            FirstName = group.Key.FirstName,
            LastName = group.Key.LastName,
            Qty = group.Count()
        })
        .ToList();

    return results;
}

class StudentData
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
    public int Qty { get; set; }
}

暂无
暂无

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

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