我想將DataTable和List之間的LINQ結合結果結合起來。將數據表和列表結合後使用DataTable和列表結合結果<Object>
這只是正常:
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.URL
或LandingPage.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)
}
);
這是url
和purchased
結果值:
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()
你爲什麼不使用LINQ語法(而不是擴展方法)從查詢的開始到結束?它可以簡化查詢的多個部分之間的引用。當你說「這行不通時,究竟是什麼問題?」 –
我嘗試過使用各種擴展方法配置(我認爲),並且使用'.GroupBy()'和'.Select()'似乎是唯一能夠滿足需要的方法。你能提供一個例子嗎? – jiy