反应式扩展 - 停止流动直到变量被填满

发布于 2024-11-26 19:56:11 字数 1121 浏览 0 评论 0原文

我有以下代码。我的方法有一个字符串返回类型。

public string GetRedemeptionNumber()
{

string _batchNumber = "test";
var _loadBatchName = Observable.ForkJoin(_context.QuerySingleOrDefault(
            _context.GetRedemptionsQuery().Where(x => x.ReceiveBatchName.StartsWith(_batchNumber))
            .OrderByDescending(x => x.ReceiveBatchName)
            .Take(1)))
            .Do(u => _redemptionBatch = u.FirstOrDefault())
                               .Select(x => new Unit())
                               .Finally(() =>
                               {
                                       _batchNumber = _redemptionBatch.Name;

                                      //this doesnt work since a return isnt allowed
                                       return _batchNumber;
                                   }
                               });

        _loadBatchName.Subscribe();


        return _batchNumber;
}

我的问题是这会贯穿并在设置之前返回我的 _batchNumber 。所以它返回一个空的_batchNumber。有没有办法阻止它运行到返回(在我的 .finally 之外)并等待 _batchNumber 从我的 .finally 中填充。

我的代码显然现在不起作用,因为我的 .finally 不允许返回。

I have the following code. My method has a string return type.

public string GetRedemeptionNumber()
{

string _batchNumber = "test";
var _loadBatchName = Observable.ForkJoin(_context.QuerySingleOrDefault(
            _context.GetRedemptionsQuery().Where(x => x.ReceiveBatchName.StartsWith(_batchNumber))
            .OrderByDescending(x => x.ReceiveBatchName)
            .Take(1)))
            .Do(u => _redemptionBatch = u.FirstOrDefault())
                               .Select(x => new Unit())
                               .Finally(() =>
                               {
                                       _batchNumber = _redemptionBatch.Name;

                                      //this doesnt work since a return isnt allowed
                                       return _batchNumber;
                                   }
                               });

        _loadBatchName.Subscribe();


        return _batchNumber;
}

My issue is this runs through and return my _batchNumber before it is set. so it is returning an empty _batchNumber. Is there a way to stop this from running to the return (outside of my .finally) and wait for the _batchNumber to be filled from within my .finally.

My code clearly doesnt work right now since my .finally does not allow a return in it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

So要识趣 2024-12-03 19:56:11

您的代码确实有一些问题。

首先,如果您想阻塞直到 Rx 返回,那么使用 Rx 就没有多大意义。应该是不阻塞。因此,您实际上应该返回 IObservable而不是 string

另外,为了使您的查询具有 OrderByDescending 功能,您通常会使用可枚举的而不是可观察的,因此您的查询必须以某种方式转换为可观察的。

并且 ForkJoin 在此查询中似乎没有为您做任何事情。你为什么使用它?

所有的 Do & Finally 运算符也没有多大意义。

无论如何,我无法将您的代码重构为可编译的代码,但我已经接近了。从这里开始,告诉我需要改变什么。

public static IObservable<string> GetRedemeptionNumber()
{
    var _batchNumber = "test";

    var q =
        _context.GetRedemptionsQuery()
        .Where(x => x.ReceiveBatchName.StartsWith(_batchNumber))
        .OrderByDescending(x => x.ReceiveBatchName)
        .Take(1);

    return (from u in _context.QuerySingleOrDefault(q)
            from z in u
            select z.Name).Take(1);
}

Your code does have a few problems.

First up if you want to block until Rx returns then there isn't much point using Rx. It should be about not blocking. So you should really return IObservable<string> and not string.

Also, for your query to have OrderByDescending you much be using an enumerable and not an observable so somehow your query must turn into an observable.

And ForkJoin doesn't seem to do anything for you in this query. Why did you use it?

All of the Do & Finally operators don't make much sense either.

In any case, I couldn't refactor your code into something that compiles, but I got it close. Start with this and tell me what needs to change.

public static IObservable<string> GetRedemeptionNumber()
{
    var _batchNumber = "test";

    var q =
        _context.GetRedemptionsQuery()
        .Where(x => x.ReceiveBatchName.StartsWith(_batchNumber))
        .OrderByDescending(x => x.ReceiveBatchName)
        .Take(1);

    return (from u in _context.QuerySingleOrDefault(q)
            from z in u
            select z.Name).Take(1);
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文