您可以根据范围创建分组吗?
这与另一个问题相关,其中我认为确实解决了一个更简单的问题,所以我在这里问更简单的问题,希望它能帮助我解决更复杂的问题。
我希望能够在 linq to sql 查询中创建一个分组,该分组基于另一组数据中的一系列数据。不过,我认为它在 linq to object 中应该同样有效,所以我们就这样吧。
想象一下,您有两个包含值的列表
{100, 110, 120, 130, 140, 150, 160, 170}
{115, 145, 180}
现在,我想将第一个列表按第二个列表分组为范围(每个组之间的值)。也就是说,我想要这样的分组(隐含 0):
{0} {100, 110}
{115} {120, 130, 140}
{145} {150, 160, 170}
{180}
我几乎可以肯定我滥用了术语,并且可能误解了 linq group by 运算符的工作原理,但如果您明白我的意思,我希望得到一些建议。谢谢。
This is related to Another Question, which I think really gets at a much simpler problem so I'm asking the simpler question here in the hopes it will help me solve the more complex one.
I would like to be able to create a grouping in a linq to sql query that groups based on a range of data within another set of data. However, i think it should work just as well in linq to objects, so let's just go with that.
Imagine you have two lists containing values
{100, 110, 120, 130, 140, 150, 160, 170}
{115, 145, 180}
Now, I would like to group the first list by the second as ranges (values that are between each group). That is, I would like a grouping like this (the 0 is implied):
{0} {100, 110}
{115} {120, 130, 140}
{145} {150, 160, 170}
{180}
I'm almost certain i'm misusing terminology, and probably have a misunderstanding of how linq group by operator works, but if you get what I mean, I'd love some suggestions. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
嗯,您当然可以在 LINQ 中轻松地表达它:
但我非常怀疑这在 LINQ to SQL 中是否有效。基本上,您必须找到一种将值映射到其中一个类别的简单方法。
完整示例:
输出:
请注意,这不会包含任何没有任何值的类别。
另请注意,这会更简单(使用
First()
代替Last()
),如果您愿意稍微不同地进行分类:换句话说,如果类别是由高于行值的第一个范围值定义的。
编辑:这是一个提供空组的版本。但在我看来,这非常可怕:
Well, you can certainly express it in LINQ easily:
But I very much doubt that that will work in LINQ to SQL. Basically you've got to find a simple way of mapping a value to one of those categories.
Complete example:
Output:
Note that this won't include any categories which don't have any values in.
Also note that this would be simpler (using
First()
instead ofLast()
) if you'd be happy to categorize slightly differently:In other words, if the category was defined by the first range value higher than the row value.
EDIT: Here's a version which gives the empty groups. It's pretty horrible though, IMO: