使用DataTable和List <object> </object>在join和group by之后组合结果

时间:2013-01-22 14:58:42

标签: c# .net linq datatable

我想在DataTable和List之间组合LINQ连接的结果。

这很好用:

var lpYear = (
    from a in _ds.Tables[0].AsEnumerable()
    join b in LandingPages on a["OFFERINGKEY"].ToString() equals b.Code into c
    from d in c.DefaultIfEmpty()
    where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("January 1, " + year)
    where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("December 31, " + year)
    where LandingPages.Any(x => x.Code == a["OFFERINGKEY"].ToString())
    orderby d.Title
    select new {
        title = d.Title,
        price = a["PRICE"]
    }).GroupBy(o => o.title)
    .Select(o => new { 
        total = o.Sum(p => decimal.Parse(p.price.ToString())), 
        count = o.Count(),
        title = o.Key
    }
);

我最终得到的行包含“total | count | title”。

我想要做的是添加更多列。例如,LandingPage.URLLandingPage.Code。我尝试过这样,但它不起作用:

var lpYear = (
    from a in _ds.Tables[0].AsEnumerable()
    join b in LandingPages on a["OFFERINGKEY"].ToString() equals b.Code into c
    from d in c.DefaultIfEmpty()
    where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("January 1, " + year)
    where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("December 31, " + year)
    where LandingPages.Any(x => x.Code == a["OFFERINGKEY"].ToString())
    orderby d.Title
    select new {
        title = d.Title,
        price = a["PRICE"],
        url = d.URL,
        code = d.Code
    }).GroupBy(o => o.title)
    .Select(o => new { 
        total = o.Sum(p => decimal.Parse(p.price.ToString())), 
        count = o.Count(),
        title = o.Key,
        url = o.Select(p=>p.url),
        code = o.Select(p=>p.code)
    }
);

这是urlpurchased的结果值:

System.Linq.Enumerable+WhereSelectEnumerableIterator`2[<>f__AnonymousType2`3[System.String,System.Object,System.String],System.String]

解决方案 (感谢Cédric Bignon

.First()放在o.Select(p=>p.url)行的末尾:

url = o.Select(p=>p.url).First(),
code = o.Select(p=>p.code).First()

1 个答案:

答案 0 :(得分:1)

忘了ToList(),在o.Select(...)

时枚举...枚举
var lpYear = (
    from a in _ds.Tables[0].AsEnumerable()
    join b in LandingPages on a["OFFERINGKEY"].ToString() equals b.Code into c
    from d in c.DefaultIfEmpty()
    where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("January 1, " + year)
    where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("December 31, " + year)
    where LandingPages.Any(x => x.Code == a["OFFERINGKEY"].ToString())
    orderby d.Title
    select new {
        title = d.Title,
        url = d.URL,
        price = a["PRICE"],
        purchased = a["PURCHASEDATE"].ToString()
    }).GroupBy(o => o.title)
    .Select(g => new { 
        total = g.Sum(p => decimal.Parse(p.price.ToString())), 
        count = g.Count(),
        title = g.Key,
        url = g.Select(p=>p.url).Distinct().Single(),
        code = g.Select(p=>p.code).Distinct().Single()
    }
);

纯粹的LINQ:

var lpYear = from o in (from a in _ds.Tables[0].AsEnumerable()
                        join b in LandingPages on a["OFFERINGKEY"].ToString() equals b.Code into c
                        from d in c.DefaultIfEmpty()
                        where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("January 1, " + year)
                        where DateTime.Parse(a["PURCHASEDATE"].ToString()) >= DateTime.Parse("December 31, " + year)
                        where LandingPages.Any(x => x.Code == a["OFFERINGKEY"].ToString())
                        orderby d.Title
                        select new 
                        {
                            title = d.Title,
                            url = d.URL,
                            price = a["PRICE"],
                            purchased = a["PURCHASEDATE"].ToString()
                        })
             group o by o.title into g
             select new 
             { 
                 total = g.Sum(p => decimal.Parse(p.price.ToString())), 
                 count = g.Count(),
                 title = g.Key,
                 url = (from p in g
                        select p.url).Distinct().Single(),
                 code = (from p in g
                         select p.code).Distinct().Single()
             };