在C#中的LINQ中按多列分组 [英] Group By Multiple Column In LINQ in C#

查看:86
本文介绍了在C#中的LINQ中按多列分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个像这样的课:

public class ActualClass
{
    public string BookName { get; set; }
    public string IssuerName { get; set; }
    public DateTime DateOfIssue { get; set; }
    public bool Status { get; set; }
}

表中包含以下数据:

It has following data in the table:

对于以下viewModel类,我想按 IssuerName DateOfIssue 将它们分组:

I would like to group them by IssuerName and DateOfIssue for the following viewModel class:

public class ViewModel
{
   public string IssuerName { get; set; }
   public DateTime DateOfIssue { get; set; }
   public List<string> Books { get; set; }
}

数据将显示如下:(成功分组后,屏幕快照数据将替换为先前的表数据)

And data will be displayed as follows: (Screenshot data will be replaced by the previous table data after successful grouping)

特别注意: 根据我的期望,ViewModel是否出了什么问题?

在遵循一些stackoverflow答案之后,我尝试了很多,但是没有一个对我有用.任何帮助将不胜感激.

I tried a lot after following some stackoverflow answers but none did work for me. Any help will be highly appreciated.

我尝试过的代码:

var viewmodel = from b in db.BookIssues
                group b by new
                {
                    b.IssuerName,
                    b.DateOfIssue
                }
                into g
                select new ViewModel()
                {
                    Name = g.key.IssuerName,
                    DateOfIssue = g.Key.DateOfIssue,
                    Books = g.ToList() //Actually this line of code is not working
                };

推荐答案

Books = g.ToList()//实际上这行不起作用

Books = g.ToList() //Actually this line of is not working

可能是因为 Books 属性的类型是List<string>,而不是List<ActualClass>.

Probably because Books property is type of List<string>, not List<ActualClass>.

能否请您尝试以下查询,我添加了b.Select(bn => bn.BookName).ToList()以仅提取图书名称:

Can you please try this query, I added b.Select(bn => bn.BookName).ToList() to extract only names of books:

var books = new List<ActualClass>
{
   new ActualClass { BookName = "A", DateOfIssue = new DateTime(2015, 10, 10, 10, 10, 0), IssuerName = "1" },
   new ActualClass { BookName = "B", DateOfIssue = new DateTime(2015, 10, 10, 10, 10, 0), IssuerName = "1" },
   new ActualClass { BookName = "C", DateOfIssue = new DateTime(2015, 10, 10, 10, 10, 0), IssuerName = "1" },
   new ActualClass { BookName = "D", DateOfIssue = new DateTime(2015, 10, 10, 10, 10, 0), IssuerName = "2" },
   new ActualClass { BookName = "E", DateOfIssue = new DateTime(2015, 10, 10, 12, 10, 0), IssuerName = "2" },
   new ActualClass { BookName = "F", DateOfIssue = new DateTime(2015, 10, 10, 12, 10, 0), IssuerName = "2" }
};

var result = books.GroupBy(x => new { x.IssuerName, x.DateOfIssue })
                .Select(b => new ViewModel
                {
                    Books = b.Select(bn => bn.BookName).ToList(),
                    // Accessing to DateOfIssue and IssuerName from Key.
                    DateOfIssue = b.Key.DateOfIssue,
                    IssuerName = b.Key.IssuerName
                });

我按以下类别分组:x.IssuerName, x.DateOfIssue.我通过以下方式在GroupBy()中传递匿名类型来做到这一点:x => new { x.IssuerName, x.DateOfIssue }.

I grouped by: x.IssuerName, x.DateOfIssue. I did that by passing anonymous type in GroupBy() with following manner: x => new { x.IssuerName, x.DateOfIssue }.

现在它们已成为关键,您可以从SELECT语句中的KEY访问 IssuerName DateOfIsssue ,如下所示:b.Key.IssuerNameb.Key.DateOfIssue.

Now they are in key and you can access to IssuerName and DateOfIssue from KEY in SELECT statement like in following: b.Key.IssuerName and b.Key.DateOfIssue.

这篇关于在C#中的LINQ中按多列分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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