gpt4 book ai didi

c# - RX challenge 5 复杂事件处理使用LINQ语言集成查询(剧透警告)

转载 作者:太空宇宙 更新时间:2023-11-03 13:55:41 25 4
gpt4 key购买 nike

complex event processing workshop在 RX 站点上,挑战 5 是使用缓冲区完成的。我有一个使用 LINQ 点或兰巴符号的解决方案。出于兴趣,我想将其转换为 LINQ 语言集成查询符号。

接下来是挑战和我的代码。由于某些原因 result2 无法正常工作,它使 UI 无响应并且输出看起来被截断了。这是不是有点奇怪,是我的问题吗,你能解决吗?

挑战(下载 here)

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;
}

最佳答案

这两种解决方案都多次订阅引号(甚至超过两次 - 请记住,多个 from 子句会在幕后导致 SelectMany 调用),所以那里已经有问题了:-)。再试一次。

关于c# - RX challenge 5 复杂事件处理使用LINQ语言集成查询(剧透警告),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12280233/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com