【问题标题】:How to count group of records using LINQ Query?如何使用 LINQ 查询计算记录组?
【发布时间】:2016-04-06 15:23:51
【问题描述】:

我有一个结构如下的数据库表:

  • ID 整数自动增量
  • 名称 VarChar
  • 状态 VarChar

样本记录:

ID      Name            Status
1       record 1        Outstanding
2       record 2        Outstanding
3       record 3        Aging
4       record 4        Outstanding
5       record 5        Aging
6       record 6        Outstanding

在表格中,主要有两种状态:“杰出”和“老化”。我想统计表中有多少条状态为“Outstanding”的记录以及多少条状态为“Aging”的记录。

这是一个示例 LINQ 查询:

Using DC = DataClassesDataContext.Create()
        Dim dataTable = From Count(Outstanding), Count(Aging) In DC.MyTable _
                        Where item.Status = "Outstanding" OrElse item.Status = "Aging" _
                        Group By item.Status _
                        Select item
End Using

预期的结果应该是:

Outstanding     Aging
4               2

你能帮我设计一个 LINQ 来实现结果吗?

【问题讨论】:

    标签: vb.net linq


    【解决方案1】:

    试试这个:

    Dim MyTable = { _
        New With {.ID = 1, .Name = "record 1", .Status = "Outstanding"},
        New With {.ID = 2, .Name = "record 2", .Status = "Outstanding"},
        New With {.ID = 3, .Name = "record 3", .Status = "Aging"},
        New With {.ID = 4, .Name = "record 4", .Status = "Outstanding"},
        New With {.ID = 5, .Name = "record 5", .Status = "Aging"},
        New With {.ID = 6, .Name = "record 6", .Status = "Outstanding"}
    }
    
    Dim dataTable = _
        From item In MyTable
        Group By Key = item.Status Into Xs = Group
        Select New With {.Status = Key, .Count = Xs.Count()}
    

    我得到这个结果:

    【讨论】:

    • 太棒了!再多一点,可以多加一个统计表中所有记录的结果吗?
    • @Trind07 - 应该是MyTable.Count()
    • 如果返回值不是列表,MyTable.Count() 将产生错误。但你的答案是正确的。
    • @Trind07 - 如果“返回值不是列表”是什么意思? MyTable 绝对是一个列表。
    • 啊抱歉我的想法有误!我的意思是 dataTable.Count()。对此感到抱歉。
    【解决方案2】:

    试试这个:-

    using System;
    using System.Collections.Generic;
    using System.Linq;
    
    
    public class Program
    {
        public static void Main()
        {   
            var test = new List<Sample>();
    
            test.Add(new Sample{ID=1,Name="record 1",Status="Outstanding"});
            test.Add(new Sample{ID=2,Name="record 2",Status="Outstanding"});
            test.Add(new Sample{ID=3,Name="record 3",Status="Aging"});
            test.Add(new Sample{ID=4,Name="record 4",Status="Outstanding"});
            test.Add(new Sample{ID=5,Name="record 5",Status="Outstanding"});
            test.Add(new Sample{ID=6,Name="record 6",Status="Aging"});
    
    
            var result = from row in  test
                                group row by "Count" into g
                                where g.FirstOrDefault() != null
                                select new
                        {
                                //Status = g.Key,
                                Outstanding = g.Where(C => C.Status == "Outstanding").Count(),
                                Aging = g.Where(C => C.Status == "Aging").Count()
                        };
    
            Console.WriteLine("Outstanding"+"   "+"Aging");
            foreach(var item in result)
            {
                Console.WriteLine(" "+item.Outstanding+"            "+item.Aging);
            }
    
        }   
    }
    
    public class Sample
    {
        public int ID {get;set;}
        public string Name {get;set;}
        public string Status {get; set;}
    }
    

    结果:-

    您可以使用以下链接运行上述示例代码 - https://dotnetfiddle.net/xC2NXm

    建议改进:)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-01
      • 2021-12-03
      • 1970-01-01
      • 1970-01-01
      • 2023-02-26
      相关资源
      最近更新 更多