【问题标题】:Linq to Sql Group By without countLinq to Sql Group By 不计其数
【发布时间】:2016-07-22 17:47:06
【问题描述】:

我有一个 linq to sql,并且一直在研究如何使用 linq to sql 对结果进行分组。我只看到其中包含计数和总和的样本。我的模型是每个客户订单都有各种注释,并且可能有多个注释。现在它正在列出所有客户订单,如果它有多个注释,则列出多次。

如何在没有总和/计数聚合的情况下在 Linq to Sql 中使用 group by

我试过了:

public IQueryable<object> getAllamcase()
    {
        try
        {
            var q = (from c in _context.Customer
                     join am in _context.table2 on c.id equals am.id
                     join ampn in _context.table3 on am.id equals ampn.id
                     join ay in _context.tabl4 on am.id equals ay.id
                     join oim in _context.table5 on am.id equals oim.id
                     group c.FileNum by new
                     {
                         FileNum = c.order,
                         assignmentdt = am.Assignment_DT,
                         oimname = oim.FullName,
                         notes = ampn.ProgressNotes,
                         years = ay.AMYear
                     }).AsQueryable();

            return q;
        }
        catch (Exception ex)
        {
            _logger.LogError("Could not get......", ex);
            return null;
        }
    }

我的结果看起来像多个 jsons

Customer  notes
1               notes 1
1               notes 2
1               notes 3
2               notes 1
2               notes 2  

我只希望它以一个 json 格式返回

 Customer  notes
1              notes 1
               notes 2
               notes 3

2              notes 1
               notes 2

【问题讨论】:

  • 不清楚您要做什么。如果您不需要 .Count 属性,请不要使用它。明确你得到什么输出以及你想要什么。
  • @JamesCurran,我已经更新了我想要的结果。
  • 你显然还没有发现导航属性。诸如Customer.OrdersOrder.Notes 之类的东西。他们马上给你你想要的。顺便说一句,我希望这些不是你的真实表名。

标签: linq linq-to-sql group-by


【解决方案1】:

你的问题不清楚,但正如我@GertArnold 所说,如果你想加载客户的笔记,你应该使用导航属性。另外,请查看naming conventions。如果您正确命名变量等,您的代码会更清晰。但是根据您的问题标题,我可以建议您关注。假设你有 Note 类:

public class Note
{
    public int CustomerId { get; set; }
    public string NoteName { get; set; }
}

你有如下的笔记列表:

List<Note> notes = new List<Note>
{
    new Note { CustomerId = 1, NoteName = "note 1" },
    new Note { CustomerId = 1, NoteName = "note 2" },
    new Note { CustomerId = 1, NoteName = "note 3" },
    new Note { CustomerId = 1, NoteName = "note 4" },
    new Note { CustomerId = 2, NoteName = "note 1" },
    new Note { CustomerId = 2, NoteName = "note 2" },
    new Note { CustomerId = 3, NoteName = "note 1" },
};

如果您想从此列表中获取 CustomerId-s 和相关注释,您可以通过对它们进行分组来轻松实现:

var result = notes
    .GroupBy(m => m.CustomerId)
    .Select(g => new
    {
        CustomerId = g.Key,
        Notes = g.Select(m => m.NoteName).ToList()
    });

结果将是:

CustomerId  ||  NoteName
1               note 1
                note 2
                note 3
                note 4
2               note 1
                note 2
3               note 1

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2010-12-31
    • 2010-11-15
    • 1970-01-01
    • 1970-01-01
    • 2016-05-26
    • 2011-04-18
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多