如何对可由键可观察的反应式对象进行分区并将分区合并到组中,组合组每个分区的最后一个元素



我有一个项目的热可观察对象序列,这些项目具有标识特定子流的键。我有兴趣将这些 M 流映射到 N 个 N 个 M <(将它们分组到 N 个桶中)。对于每个存储桶,每次元素到达时,我都想将该组的每个下划线序列的最新元素应用一个函数。我之前对N组和M组都有了解。

在下面的示例中,我们有四种水果的报价序列。我想按水果类型(苹果或梨)将这些溪流一分为二。对于每个组,我想收集每个水果的最后已知报价。

class Input {
    public string ProductID {get;set;}
    public string ProductType {get;set;}
    public int    Price {get;set;}
}
class Output {
    public string  ProductType {get;set;}
    public Input[] Underlining {get;set;}
}
var obs = new List<Input> {
    new Input { ProductID = "Stark",    ProductType = "Apple", Price = 21 },
    new Input { ProductID = "Jonagold", ProductType = "Apple", Price = 12 },
    new Input { ProductID = "Williams", ProductType = "Pear",  Price = 33 },
    new Input { ProductID = "Beth",     ProductType = "Pear",  Price = 22 },
    new Input { ProductID = "Stark",    ProductType = "Apple", Price = 43 },
    new Input { ProductID = "Williams", ProductType = "Pear",  Price = 55 },
    new Input { ProductID = "Beth",     ProductType = "Pear",  Price = 66 },
    new Input { ProductID = "Jonagold", ProductType = "Apple", Price = 77 },
    new Input { ProductID = "Jonagold", ProductType = "Apple", Price = 25 },
    new Input { ProductID = "Williams", ProductType = "Pear",  Price = 77 },
    new Input { ProductID = "Beth",     ProductType = "Pear",  Price = 13 },
    new Input { ProductID = "Stark",    ProductType = "Apple", Price = 21 },
}.ToObservable();
IObservable<Output> result = obs.GroupBy ... Select ... Concat ... ; // I'm a bit loss here 

result.Dump();

预期成果:

{ ProductType = "Apple", Underlining = [{ ProductID = "Stark",    Price = 21 }] }
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark",    Price = 21 },     { ProductID = "Jonagold", Price = 12 }] }
{ ProductType = "Pear",  Underlining = [{ ProductID = "Williams", Price = 23 }] }
{ ProductType = "Pear",  Underlining = [{ ProductID = "Williams", Price = 23 },     { ProductID = "Beth",     Price = 22 }] }
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark",    Price = **43** }, { ProductID = "Jonagold", Price = 12 }] }
{ ProductType = "Pear",  Underlining = [{ ProductID = "Williams", Price = **55** }, { ProductID = "Beth",     Price = 22 }] }
{ ProductType = "Pear",  Underlining = [{ ProductID = "Williams", Price = 55 },     { ProductID = "Beth",     Price = **66** }] }
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark",    Price = 43 },     { ProductID = "Jonagold", Price = **77** }] }
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark",    Price = 43 },     { ProductID = "Jonagold", Price = **25** }] }
{ ProductType = "Pear",  Underlining = [{ ProductID = "Williams", Price = **77** }, { ProductID = "Beth",     Price = 66 }] }
{ ProductType = "Pear",  Underlining = [{ ProductID = "Williams", Price = 77 },     { ProductID = "Beth",     Price = **13** }] }
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark",    Price = **21** }, { ProductID = "Jonagold", Price = 25 }] }

我认为这就是你想要的:

var outputs =
    obs
        .GroupBy(x => x.ProductType)
        .Select(xs =>
            xs
                .Scan(
                    new Dictionary<string, Input>(),
                    (d, x) => { d[x.ProductID] = x; return d; })
                .Select(x => new Output()
                {
                    ProductType = xs.Key,
                    Underlining = x.Values.ToArray(),
                }))
        .Merge();

我使用outputs.Select(x => $"{{ ProductType = "{x.ProductType}", Underlining = [{String.Join(", ", x.Underlining.Select(y => $"{{ ProductID = "{y.ProductID}", Price = {y.Price} }}"))}] }}")来获取以下输出来测试它:

{ ProductType = "Apple", Underlining = [{ ProductID = "Stark", Price = 21 }] } 
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark", Price = 21 }, { ProductID = "Jonagold", Price = 12 }] } 
{ ProductType = "Pear", Underlining = [{ ProductID = "Williams", Price = 33 }] } 
{ ProductType = "Pear", Underlining = [{ ProductID = "Williams", Price = 33 }, { ProductID = "Beth", Price = 22 }] } 
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark", Price = 43 }, { ProductID = "Jonagold", Price = 12 }] } 
{ ProductType = "Pear", Underlining = [{ ProductID = "Williams", Price = 55 }, { ProductID = "Beth", Price = 22 }] } 
{ ProductType = "Pear", Underlining = [{ ProductID = "Williams", Price = 55 }, { ProductID = "Beth", Price = 66 }] } 
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark", Price = 43 }, { ProductID = "Jonagold", Price = 77 }] } 
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark", Price = 43 }, { ProductID = "Jonagold", Price = 25 }] } 
{ ProductType = "Pear", Underlining = [{ ProductID = "Williams", Price = 77 }, { ProductID = "Beth", Price = 66 }] } 
{ ProductType = "Pear", Underlining = [{ ProductID = "Williams", Price = 77 }, { ProductID = "Beth", Price = 13 }] } 
{ ProductType = "Apple", Underlining = [{ ProductID = "Stark", Price = 21 }, { ProductID = "Jonagold", Price = 25 }] } 

相关内容

  • 没有找到相关文章

最新更新