下午,
我正在尝试从SQL Server 2008 R2数据库返回产品列表,但我只想返回最新日期的结果。
然而,下面的代码似乎返回所有项目,即使是日期较旧的项目。如何仅使用最新日期获取结果。
var query = (from a in dc.aboProducts
join t in dc.twProducts on a.sku equals t.sku
join lp in dc.LowestPrices on a.asin equals lp.productAsin
select new GetLowestPrices
{
productAsin = lp.productAsin,
sku = t.sku,
title = t.title,
twprice = Convert.ToString(t.twPrice),
lowprice = Convert.ToString(lp.price),
amzprice = Convert.ToString(lp.tweAmzPrice),
lastupdated = Convert.ToDateTime(lp.priceDate)
}).Distinct().OrderBy(ti => ti.title);
return query.ToList();
答案 0 :(得分:0)
尝试这样的事情:
var query =
from a in dc.aboProducts
join t in dc.twProducts on a.sku equals t.sku
join lp in dc.LowestPrices on a.asin equals lp.productAsin
select new GetLowestPrices
{
productAsin = lp.productAsin,
sku = t.sku,
title = t.title,
twprice = Convert.ToString(t.twPrice),
lowprice = Convert.ToString(lp.price),
amzprice = Convert.ToString(lp.tweAmzPrice),
lastupdated = Convert.ToDateTime(lp.priceDate)
};
var lookup =
query
.ToLookup(x => x.sku)
.Select(x => x.OrderByDescending(y => y.lastupdated).First())
.OrderBy(x => x.title);
return lookup.ToList();
答案 1 :(得分:0)
因为Distinct未应用于现有列表,但创建一个新列表,您需要将其作为新列表获取:
val DistinctList = ExistingList.Distinct().ToList();