Linq group by,按值排序

时间:2016-11-10 10:17:22

标签: c# linq linq-to-sql

我需要帮助 我有桌子:

   Id          Day                 Group       Value 
 -------------------------------------------------
   1           2016-10-11          1           10.5
   2           2016-10-11          1           10.8
   3           2016-10-11          1           10.7
   4           2016-10-11          1           10.6
   5           2016-10-11          1           10.5
   6           2016-10-11          1           10.4
   7           2016-10-11          1           10.8
   8           2016-10-11          1           10.2
   9           2016-10-11          1           10.0
   10          2016-10-11          1           10.9
   11          2016-10-11          2           10.1
   12          2016-10-11          2           10.0
   13          2016-10-11          2           10.1
   14          2016-10-11          2           10.6
   15          2016-10-11          2           10.7
   16          2016-10-11          2           10.2
   17          2016-10-11          2           10.0
   18          2016-10-11          2           10.5
   19          2016-10-11          2           10.5
   20          2016-10-11          2           10.8
   21          2016-10-12          1           11.1
   22          2016-10-12          1           11.7
   23          2016-10-12          1           11.0
   24          2016-10-12          1           11.4
   25          2016-10-12          1           11.7
   26          2016-10-12          1           11.8
   27          2016-10-12          1           11.1
   28          2016-10-12          1           11.1
   29          2016-10-12          1           11.4
   30          2016-10-12          1           11.6
   31          2016-10-12          2           11.9 
   32          2016-10-12          2           11.6
   ...

我想要

  • 按价值排序
  • group by Group
  • 按天分组
  • 并且只获得如下值:

[[[10.5,10.8],[10.0,10.1]],[[11.1,11.7],[11.6,11.9]]](示例)

我需要更多地使用这个值,当我获得价值时,我会在Day获得更多价值。然后我将这批次分成4批,然后我从每个组中选择第一个值。因此,我将为每个组提供4个值,对于1天,我将有2个批次,每个批次将具有4个值。并且因为我需要在每个批次中都有5个值,如最后一个值,我找到Max Value并将其添加到具有4个值的批处理中。我不知道你是否理解我的代码:

    var valueList = await Task.Factory.StartNew(() =>

    (
        from pv in db.Values
        where DbFunctions.TruncateTime(pv.Day) >= startDate      
        && DbFunctions.TruncateTime(pv.Day) <= endDate           
        orderby pv.Value
        //group pv by pv.Day into gpv
        select pv
    ));

var group1 = await Task.Factory.StartNew(() => (
(
    from vl in valueList
    where vl.Group == 1
    select vl) 
));

var group2 = await Task.Factory.StartNew(() =>
(
    from vl in valueList
    where vl.Group == 2
    select vl
));

var group3 = await Task.Factory.StartNew(() =>
(
    from vl in valueList
    where vl.Group == 3
    select vl 
));


var group1Split = group1.Split(group1.Count() / 4 + 1).Select(x => x.First());
var group1Max = group1.Max();
var group2Split = group2.Split(group2.Count() / 4 + 1).Select(x => x.First());
var group2Max = group2.Max();
var group3Split = group3.Split(group3.Count() / 4 + 1).Select(x => x.First());
var group3Max = group3.Max();

var group1Values = group1Split.Concat(group1Max);
var group2Values = group2Split.Concat(group2Max);
var group3Values = group3Split.Concat(group3Max);

var groupAllValues = group1Values.Concat(group2Values).Concat(group3Values);
var groupSplitAllValues = groupAllValues.Split(5);

return Request.CreateResponse(HttpStatusCode.OK, groupSplitAllValues.ToList());

更新

这项工作:

var result = db.Values
    .Where(x => (DbFunctions.TruncateTime(x.Day)) >= startDate
    && (DbFunctions.TruncateTime(x.Day)) <= endDate)
   .GroupBy(x => new { Day = x.Day, Group = x.Group })
   .Select(x => x.Select(y => y.Value).OrderBy(z => z).ToList()).ToList();

我明白了:

[
[10.0, 10.2, 10.4, 10.5, 10.5, 10.6, 10.7, 10.8, 10.8, 10.9],
[10.0, 10.0, 10.1, 10.1, 10.2, 10.5, 10.5, 10.6, 10.7, 10.8],
[11.0, 11.1, 11.1, 11.1, 11.4, 11.4, 11.6, 11.7, 11.7, 11.8],
[...]
]

但是我需要将每个批次拆分为2个部分并从该部分中选择第一个值。所以我将得到2个值,然后我想从这个批次得到concat到2个值的最大值,所以对于这个例子,我的最终结果将是:

[
[10.0, 10.6, 10.9],
[10.0, 10.2, 10.8],
[11.0, 11.4, 11.8],
[...]
]

对于拆分我使用这种方法:

public static IEnumerable<IEnumerable<T>> Split<T>(this IEnumerable<T> source, int length)
        {
            if (length <= 0)
                throw new ArgumentOutOfRangeException("length");

            var section = new List<T>(length);

            foreach (var item in source)
            {
                section.Add(item);

                if (section.Count == length)
                {
                    yield return section.AsReadOnly();
                    section = new List<T>(length);
                }
            }

            if (section.Count > 0)
                yield return section.AsReadOnly();
        }

2 个答案:

答案 0 :(得分:1)

试试这个

_doc.on('click','.close_btn',function(e) {
   jQuery(this).closest('.parent_div').hide();
});

答案 1 :(得分:0)

以下是我如何做到这一点的示例,我确信必须有一种更优雅的方式来生成输出字符串

static void Main(string[] args)
    {
        List<Example> samples = new List<Example>();
        samples.Add(new Example(1, DateTime.Parse("2016-10-11"), 1, 10.5));
        samples.Add(new Example(2, DateTime.Parse("2016-10-11"), 1, 10.8));
        samples.Add(new Example(3, DateTime.Parse("2016-10-11"), 2, 10.1));
        samples.Add(new Example(4, DateTime.Parse("2016-10-11"), 2, 10.0));
        samples.Add(new Example(5, DateTime.Parse("2016-10-12"), 1, 11.1));
        samples.Add(new Example(6, DateTime.Parse("2016-10-12"), 1, 11.7));
        samples.Add(new Example(7, DateTime.Parse("2016-10-12"), 2, 11.9));
        samples.Add(new Example(8, DateTime.Parse("2016-10-12"), 2, 11.6));

        var daily_results = samples.GroupBy(g => new { g.group, g.date }).GroupBy(g => g.Key.date);

        StringBuilder sb = new StringBuilder();

        sb.Append("[");
        foreach (var group_result in daily_results)
        {
            sb.Append("[");
            foreach(var result in group_result)
            {
                sb.Append($"[{string.Join(",", result.OrderBy(r => r.value).Select(r => r.value))}]");
            }
            sb.Append("]");
        }
        sb.Append("]");

        Console.WriteLine(sb);
        Console.Read();
    }
}
class Example
{
    public int id;
    public DateTime date;
    public int group;
    public double value;
    public Example(int i, DateTime d, int g, double v)
    {
        id = i;
        date = d;
        group = g;
        value = v;
    }
}

更新:输出代码可以简化如下:

        var daily_results = samples.GroupBy(g => new { g.group, g.date }).GroupBy(g => g.Key.date);

        string output = string.Empty;

        daily_results.ToList().ForEach(grp =>
            {
                output += "[";
                grp.ToList().ForEach(res =>
                {
                    output += $"[{string.Join(",", res.OrderBy(r => r.value).Select(r => r.value))}],";
                });
                output = output.TrimEnd(',') + "],";
            });
        Console.WriteLine($"[{output.TrimEnd(',')}]");
        Console.Read();
相关问题