我有以下查询。 :
List<MyItem> myList
= randomEntity
.GroupBy(x => new {
x.Id,
x.randomInnerEntity.Name,
x.randomInnerEntity.Number
})
.Select(z => new MyItem
{
Id = z.Key.Id,
Name = z.Key.Name,
Totals = z.Count(),
LatestObj = randomEntity.Where(x => x.Id == z.Key.Id)
.GroupBy(x => x.Id)
.Select(gr => new {
item1 = gr.Key,
item2 = gr.Max(x => x.SomeInteger.Value)
})
})
.OrderBy(z => z.Name)
.ToList();
正如您可以看到内部子查询,它提供了LatestObj的动态对象,但它提供了一个匿名对象。我想知道如何执行相同的查询但返回像整数这样的基本类型。
***根据要求
public class MyItem
{
property Guid? Id { get; set; }
property String Name { get; set; }
property Int MaxValueTryingToGet { get; set; } //This is the field I would like to set with the integer max
property Int Totals { get; set; }
property dynamic LatestObj { get; set; }
property randInner randomInnerEntity { get; set; }
}
答案 0 :(得分:1)
LatestObj = randomEntity.Where(x => x.Id == z.Key.Id)
.GroupBy(x => x.Id)
.Select(gr => new {
item1 = gr.Key,
item2 = gr.Max(x => x.SomeInteger.Value)
})
由于您只是选择商品ID与z.Key.Id
匹配的商品,因此ID似乎没有多少分组。
您可以使用
直接获取最大值LatestObj = randomEntity.Where(x => x.Id == z.Key.Id).Max(x -> x.SomeInteger.Value)