将当前项目送入LINQ查询中的下一个项目 [英] Feeding current item into next item in LINQ query

查看:101
本文介绍了将当前项目送入LINQ查询中的下一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些我想要转换的项目集合(基本上是包装在元数据中)。对于集合中的第一个项目,我只是将必要的元数据包装在项目中,然后完成。第二个项目的元数据依赖于前一个项目的元数据,因此我想将第一个项目的元数据传递给第二个项目。



以下....

  object [] A; 
$ b A.skip(1)
.Zip(A,(first,second),a => new Metadata(first,second));

...不会工作,因为它使用 object 作为第二个输入,而不是元数据。对于我目前看到的其他解决方案(传递原始对象,而不是 Select()函数的结果),这是真实的。



如何将我的LINQ查询中第一个项目的 Select()的结果提供给 Select ()为第二项?



我知道我可以使用作为循环为了实现这一点,但我想知道是否没有更有效的方式来处理这类问题。必须有功能风格语言(如F#)的解决方案,我想知道是否可以将它转换为C#中的LINQ解决方案。通过将元数据链接在一起,我可以将重要信息从一个项目传递到以下所有项目,例如作为标志或上下文。最终的目标不仅仅是了解上一个项目,而是了解当前项目的整个背景。然后当前项目可以更改所有后续项目的上下文,等等。

解决方案

这种行为通常通过 scan 或函数式编程语言中的类似函数。在F#中,你可以这样做:

  originalSequence |> Seq.scan(fun(prev,current) - > Some< | new Metadata((* ... stuff ... *)))None 

请参阅此问题 a>用于在C#中实现 Scan 扩展方法。

  A.Scan((previous,current)=>新元数据(/ * ... * /),null)

Scan 方法的内部实现。


I have a collection of items that I want to transform (basically, wrap in metadata). For the first item in the collection, I simply wrap the necessary metadata around the item and I'm done. The second item's metadata is dependent on the previous item's metadata, so I want to pass the first item's metadata to the second item.

The following....

object[] A;

A.skip(1)
 .Zip( A, (first, second), a => new Metadata( first, second ) );

...will not work because it's using object as the second input rather than Metadata. This is true for every other solution I've seen so far (passing the original object, rather than the result of the Select() function).

How would I feed the result of the Select() for the first item in my LINQ query into the Select() for the second item?

I know I could use a for loop to accomplish this, but I'm wondering if there isn't a more functional way of handling this kind of problem. There must be a solution in functional style languages such as F# and I'm wondering if I could translate that into a LINQ solution in C#. A one-liner solution would be especially awesome.

By chaining the metadata together like this, I can pass along important information from one item to all of the following items, such as flags or context. The goal, ultimately, is not just to know about the previous item, but about the whole context of the current item. The current item can then change the context for all following items, etc.

解决方案

This kind of behavior is usually accomplished with a scan or similar function in functional programming languages. In F#, you can do it like this:

originalSequence |> Seq.scan (fun (prev, current) -> Some <| new Metadata( (* ... stuff ... *) )) None

See this question for an implementation of a Scan extension method in C#. Given that method, you can implement your query like this:

A.Scan((previous, current) => new Metadata(/* ... */), null)

Can't think of an elegant one-liner implementation of the Scan method though.

这篇关于将当前项目送入LINQ查询中的下一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆