您可以根据范围创建分组吗?

发布于 2024-09-11 17:35:11 字数 618 浏览 6 评论 0原文

这与另一个问题相关,其中我认为确实解决了一个更简单的问题,所以我在这里问更简单的问题,希望它能帮助我解决更复杂的问题。

我希望能够在 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 技术交流群。

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

发布评论

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

评论(1

疏忽 2024-09-18 17:35:11

嗯,您当然可以在 LINQ 中轻松地表达它:

var x = from value in values
        group value by ranges.Where(x => value >= x)
                             .DefaultIfEmpty()
                             .Last();

但我非常怀疑这在 LINQ to SQL 中是否有效。基本上,您必须找到一种将值映射到其中一个类别的简单方法。

完整示例:

using System;
using System.Linq;
using System.Collections.Generic;

class Test
{
    static void Main()
    {
        int[] values = {100, 110, 120, 130, 140, 150, 160, 170};
        int[] ranges = {115, 145, 180};

        var query = from value in values
                    group value by ranges.Where(x => value >= x)
                                         .DefaultIfEmpty()
                                         .Last();

        foreach (var group in query)
        {
            Console.WriteLine("{0}: {{{1}}}", group.Key, 
                              string.Join(", ", group));
        }
    }
}

输出:

0: {100, 110}
115: {120, 130, 140}
145: {150, 160, 170}

请注意,这不会包含任何没有任何值的类别。

另请注意,这会更简单(使用 First() 代替Last()),如果您愿意稍微不同地进行分类:

115: {100, 110}
145: {120, 130, 140}
180: {150, 160, 170}

换句话说,如果类别是由高于行值的第一个范围值定义的。

编辑:这是一个提供空组的版本。但在我看来,这非常可怕:

var query = from range in ranges
            join value in values
            on range equals ranges.Where(x => value >= x)
                                  .DefaultIfEmpty()
                                  .Last() into groups
            select new { Key = range, Values = groups};

foreach (var group in query)
{
    Console.WriteLine("{0}: {{{1}}}", group.Key, 
                      string.Join(", ", group.Values));
}

Well, you can certainly express it in LINQ easily:

var x = from value in values
        group value by ranges.Where(x => value >= x)
                             .DefaultIfEmpty()
                             .Last();

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:

using System;
using System.Linq;
using System.Collections.Generic;

class Test
{
    static void Main()
    {
        int[] values = {100, 110, 120, 130, 140, 150, 160, 170};
        int[] ranges = {115, 145, 180};

        var query = from value in values
                    group value by ranges.Where(x => value >= x)
                                         .DefaultIfEmpty()
                                         .Last();

        foreach (var group in query)
        {
            Console.WriteLine("{0}: {{{1}}}", group.Key, 
                              string.Join(", ", group));
        }
    }
}

Output:

0: {100, 110}
115: {120, 130, 140}
145: {150, 160, 170}

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 of Last()) if you'd be happy to categorize slightly differently:

115: {100, 110}
145: {120, 130, 140}
180: {150, 160, 170}

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:

var query = from range in ranges
            join value in values
            on range equals ranges.Where(x => value >= x)
                                  .DefaultIfEmpty()
                                  .Last() into groups
            select new { Key = range, Values = groups};

foreach (var group in query)
{
    Console.WriteLine("{0}: {{{1}}}", group.Key, 
                      string.Join(", ", group.Values));
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文