唉,你没有准确地描述你的要求,只是你想要一些匿名类型并且它们应该按 Id 排序。您的查询语法与您的方法语法不同。所以我只能举一个例子来创建你的匿名对象序列
因此,您有一系列相似的项目,其中每个项目至少具有属性 Id 和 IdOperation。您希望创建项目组,其中每个组中的每个项目都具有相同的 IdOperation 值。您想通过 Id 升序对每个组中的元素进行排序,并创建一些匿名类型。
你没有在你的匿名对象中指定你想要的东西(毕竟:你的代码没有做你想要的,所以我不能从你的代码中扣除它)
每当我使用 GroupBy,并且我想指定每个组的元素时,我都会使用 overload of GroupBy that has a parameter resultSelector。使用 resultSelector 我可以精确地定义组的元素。 (链接指的是IQueryable,还有一个IEnumerable version)
IEnumerable<Operations> operations = ... // = your list
// Make Groups of Operations that have the same value for IdOperation
var result = operations.GroupBy(operation => operation.IdOperation,
// parameter resultSelector: take the key (=idOperation) and all Operations that have
// this idOperation, to make one new.
(idOperation, operationsWithThisId) => new
{
// do you need the common idOperation?
IdOperation = idOperation,
// Order the elements in each group by Id:
Operations = operationsWithThisId.OrderBy(operation => operation.Id)
.Select(operation => new
{
// Select only the operation properties that you plan to use
Id = operation.Id,
Name = operation.Name,
StartDate = operation.StartDate,
...
})
.ToList(),
});
简而言之:从您的操作序列中,创建具有相同 IdOperation 值的操作组。然后使用这个通用的 IdOperation 以及该组中的所有操作来制作一个匿名对象:这就是您所说的匿名对象。因此,每个组,您制作一个匿名对象。
- IdOperation 是该组中所有操作的共同值
- 操作是一个列表。该组中的所有操作都按 Id 升序排列。选择了几个属性并将结果放入列表中。
如果您想以不同方式分组,例如查询语法,只需更改参数 keySelector:
var result = operations.GroupBy(operation => new
{
Id = operation.ID,
IdOperation = operation.IDOperation,
IdDiagnosis = operation.IDDiagnosis
},
虽然这与您在查询语法中所做的相对应,但您将拥有一组具有相同 Id / IdOperation / IDDiagnosis 值的操作。按 Id 对组中的元素进行排序是没有用的,因为该组中的所有 Id 都将相等。
结论
使用参数 resultSelector,您可以完全按照自己的意愿定义结果:结果不是IEnumerable<IGrouping<Tkey, TElement>>,而是IEnumerable<TResult>。
TResult 是一个对象,由一组中的所有元素和公共组值创建。