【发布时间】: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.Orders、Order.Notes之类的东西。他们马上给你你想要的。顺便说一句,我希望这些不是你的真实表名。
标签: linq linq-to-sql group-by