【问题标题】:Division results from SQL to separate List <int>从 SQL 到单独的 List <int> 的除法结果
【发布时间】:2015-10-14 20:46:35
【问题描述】:

我从下面的 MySQL 表中得到结果。 每次数据的结果都是不同的,我需要将它分组到单独的新列表。 DataTable 中的数据列表:

id_task id_supplier id_source_ps
1   77  3
2   71  3
3   77  1
4   77  2
5   72  3
6   72  2
7   77  3
8   72  3
9   71  1
10  72  3

将id_task分组到新列表的条件是id_supplier和id_source_ps相同!

用什么方法(LINQ?)来得到结果——就像这个例子——七个新的List

name of list    id_task_in
group1  9
group2  2
group3  6
group4  5,8,10
group5  3
group6  4
group7  1,7

【问题讨论】:

  • 您的结果类型不是 List。这是一个列表。那是你真正想要的吗?

标签: c# mysql linq


【解决方案1】:

您正在寻找GroupBy。你需要GroupBy你的钥匙然后Select你的结果。这是使用您的数据的示例--

public class Program
{
    public static void Main(string[] args)
    {
        var list = new List<Item>
        {
            new Item { task = 1, supplier = 77, source = 3 },
            new Item { task = 2, supplier = 71, source = 3 },
            new Item { task = 3, supplier = 77, source = 1 },
            new Item { task = 4, supplier = 77, source = 2 },
            new Item { task = 5, supplier = 72, source = 3 },
            new Item { task = 6, supplier = 72, source = 2 },
            new Item { task = 7, supplier = 77, source = 3 },
            new Item { task = 8, supplier = 72, source = 3 },
            new Item { task = 9, supplier = 71, source = 1 },
            new Item { task = 10, supplier = 72, source = 3 }
        };

        var groupBy = 
            list
            .GroupBy(x => new { first = x.source, second = x.supplier})
            .Select(x => new { name = x.Key, items = x.Count() });

        foreach (var g in groupBy)
        {
            Console.WriteLine(g);
        }        
    }        
}

public class Item
{
    public int task;
    public int supplier;
    public int source;
}

打印出来

{ name = { first = 3, second = 77 }, items = 2 }
{ name = { first = 3, second = 71 }, items = 1 }
{ name = { first = 1, second = 77 }, items = 1 }
{ name = { first = 2, second = 77 }, items = 1 }
{ name = { first = 3, second = 72 }, items = 3 }
{ name = { first = 2, second = 72 }, items = 1 }
{ name = { first = 1, second = 71 }, items = 1 }

您可以将此示例作为您的起点。要完成您需要的操作,只需将 .Count() 更改为 .Aggregate(),这样它就会返回任务 ID 的列表,而不是任务 ID 的数量。如果您愿意,您还可以更改键以匹配“组 1”“组 2”,但您需要想出一些标准方法来为每个组编号(例如,"group"+(index+1).ToString())

【讨论】:

    【解决方案2】:

    基于汤姆的回答:

    public static void Main(string[] args)
    {
        var list = new List<Item>
        {
            new Item { task = 1, supplier = 77, source = 3 },
            new Item { task = 2, supplier = 71, source = 3 },
            new Item { task = 3, supplier = 77, source = 1 },
            new Item { task = 4, supplier = 77, source = 2 },
            new Item { task = 5, supplier = 72, source = 3 },
            new Item { task = 6, supplier = 72, source = 2 },
            new Item { task = 7, supplier = 77, source = 3 },
            new Item { task = 8, supplier = 72, source = 3 },
            new Item { task = 9, supplier = 71, source = 1 },
            new Item { task = 10, supplier = 72, source = 3 }
        };
    
        var groupBy = list.GroupBy(x => new { first = x.supplier, second = x.source})
          .OrderBy(x=>x.Key.first).ThenBy(x=>x.Key.second)
          .Select((x,i) => new { name = "group"+(i+1), items = x.Select(y=>y.task) });
    
        foreach (var g in groupBy)
        {
            Console.WriteLine("{0} {1}",g.name,String.Join(",",g.items.Select(x=>x.ToString())));
        }        
    }        
    
    public class Item
    {
        public int task;
        public int supplier;
        public int source;
    }
    

    将返回:

    group1 9
    group2 2
    group3 6
    group4 5,8,10
    group5 3
    group6 4
    group7 1,7
    

    我将最终输出保留为IEnumerable&lt;string,IEnumerable&lt;int&gt;&gt;,因为我通常更喜欢这样。如果您希望使用 IEnumerable&lt;string,string&gt; 代替,您可以轻松地将 string.join 移动到 select 中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-29
      • 2016-07-02
      • 1970-01-01
      • 1970-01-01
      • 2013-01-14
      • 2016-01-11
      • 2019-04-21
      • 2020-05-17
      相关资源
      最近更新 更多