您可以根据范围创建分组吗? [英] Can you create a grouping based on ranges?

查看:60
本文介绍了您可以根据范围创建分组吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这与另一个问题,我认为这确实是一个简单得多的问题,所以我在这里问一个简单的问题,希望它可以帮助我解决更复杂的问题.

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.

我希望能够在linq to sql查询中创建一个分组,该分组基于另一组数据中的一系列数据进行分组.但是,我认为它在linq中对对象同样有效,所以让我们开始吧.

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}

现在,我想将第一个列表按第二个列表进行分组,作为范围(每个组之间的值).也就是说,我想要一个这样的分组(暗含0):

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}

我几乎可以肯定我滥用术语,并且可能对linq group by运算符的工作方式有误解,但是如果您理解我的意思,我会喜欢一些建议.谢谢.

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.

推荐答案

好吧,您当然可以在LINQ中轻松表达:

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();

但是我非常怀疑这是否可以在LINQ to SQL中使用.基本上,您必须找到一种将值映射到这些类别之一的简单方法.

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.

完整示例:

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}

请注意,该不会包括任何没有任何值的类别.

Note that this won't include any categories which don't have any values in.

还请注意,如果您希望将分类稍有不同,这会更简单(使用First()代替Last()):

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.

这是一个给出空组的版本. IMO,这真是太恐怖了:

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));
}

这篇关于您可以根据范围创建分组吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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