c# - 在 C# 的 LINQ 中按多列分组

标签 c# asp.net-mvc entity-framework linq

我有一个类如下:

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

表中有如下数据: enter image description here

对于以下 viewModel 类,我想按 IssuerNameDateOfIssue 对它们进行分组:

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

并且数据会显示如下:(分组成功后截图数据会被之前的表格数据替换) enter image description here

特别注意: 我的ViewModel是否按照我的预期有什么问题?

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

我试过的代码:

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() //Actually this line of is not working

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

你能试试这个查询吗,我添加了b.Select(bn => bn.BookName).ToList()仅提取书名:

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 } .

现在它们在 key 中,您可以从 SELECT 语句中的 KEY 访问 IssuerNameDateOfIssue,如下所示:b.Key.IssuerNameb.Key.DateOfIssue .

关于c# - 在 C# 的 LINQ 中按多列分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40755567/

相关文章:

c# - Windows Phone 7 Mango 启动画面上的断断续续的渐变

ASP.NET Identity - 使用安全戳强制重新登录

c# - WinSCP .NET 程序集 - 在 GetFiles 之后删除文件(不是目录)

c# - 代码隐藏中的 GridView RowUpdating SqlDataSource.Update

html - 背景图片 url 在生产服务器上不起作用

c# - Entity Framework 6 Code First 插入时的默认日期时间值

c# - Entity Framework 中的 HttpContext 数据缓存

c# - 如何去掉 EF 添加的对象名称后缀?

c# - 使用 Entity Framework 5 和 MVC 4 的自定义查询

c# - ASP.NET MVC Excel 导出文件格式错误