在 RX 站点的复杂事件处理研讨会中,挑战 5 使用 Buffer 完成。我有一个使用 LINQ 点或 Lamba 表示法的解决方案。出于兴趣,我想将其转换为 LINQ 语言集成查询符号。
接下来是挑战和我的代码。由于某种原因result2
无法正常工作,它使 UI 无响应并且输出看起来被截断。这是什么时髦的东西,是我的查询,你能解决它吗?
挑战(在此处下载)
IObservable<object> Query(IObservable<StockQuote> quotes)
{
// TODO: Change the query below to compute the average high and average low over
// the past five trading days as well as the current close and date.
// HINT: Try using Buffer.
return from quote in quotes
where quote.Symbol == "MSFT"
select new { quote.Close, quote.Date };
}
我的解决方案
IObservable<object> Query(IObservable<StockQuote> quotes)
{
// TODO: Change the query below to compute the average high and average low over
// the past five trading days as well as the current close and date.
// HINT: Try using Buffer.
var result1 = quotes.Where(qt => qt.Symbol == "MSFT").Buffer(5, 1).Select(quoteList =>
{
var avg = quoteList.Average(qt => qt.Close);
return new { avg, quoteList.Last().Close, quoteList.Last().Date };
});
var result2 = from quote in quotes
where quote.Symbol == "MSFT"
from quoteList in quotes.Buffer(5, 1)
let avg = quoteList.Average(qt => qt.Close)
select new { avg, quoteList.Last().Close, quoteList.Last().Date };
return result2;
}