不是简单的Max

Pau*_*hra 2 c# linq-to-entities max

我有以下数据:

Id     | Value | OtherStuff
---------------------------
6      | 6     | 1
---------------------------
5      | 4     | 2
---------------------------
5      | 2     | 3
Run Code Online (Sandbox Code Playgroud)

期望的结果:

Id     | Value | OtherStuff
---------------------------
6      | 6     | 1
---------------------------
5      | 4     | 2
Run Code Online (Sandbox Code Playgroud)

那就是我需要每个Id的最大值.

我有点难以理解如何在不将其分解为多个查询的情况下执行此操作,是否可以完成,如果是这样,怎么办?

更新:我认为我过分简化了问题:

var query = from st in StockStakes
            join o in Organisations on j.OrganisationId equals o.OrganisationId into oGroup
            from o in oGroup.DefaultIfEmpty()
            where st.Stock.Status == "A"
            select new
            {
                Id = st.Id,
                Value = st.Value,
                CustomerId = o.OrganisationId
            };
Run Code Online (Sandbox Code Playgroud)

上面的数据样本仍然存在......现在我如何构建查询以给出每个Id旁边的最大值?

Adu*_*cci 5

var query = from x in data
            group x by x.Id into x
            select x.OrderByDescending(y => y.Value).FirstOrDefault()
Run Code Online (Sandbox Code Playgroud)

基于您更新的查询,与第一个查询类似的方法,但由于您有多个表,您需要将所有表组成一个匿名对象,然后只选择您想要的列

var query = from st in StockStakes
            join o in Organisations on j.OrganisationId equals o.OrganisationId into oGroup
            from o in oGroup.DefaultIfEmpty()
            where st.Stock.Status == "A"
            group new { st, o } by st.Id into g
            let largestValue = g.OrderByDescending(x => x.Value).FirstOrDefault()
            select new
            {
                Id = g.Key,
                Value = largestValue.st.Value,
                CustomerId = largestValue.o.OrganisationId
            };
Run Code Online (Sandbox Code Playgroud)