3

我有这个清单

var commonContractsList = (
        from i in referredDilutions
        join f in dilutionsList
            on i.Contract equals f.Contract
        select f)
    .ToList();

该列表commonContractsList有几个字段,特别是三个contractinstalmentamount。我们可以有几个instalments 一个contract。每个contract/instalment都有一个amount.

现在,我想要做的是为每个 s 获取contract所有记录的列表,contract其中 sinstalment具有更高的amount

源数据

contract    instalment    amount
1           1             100
1           2             1000
2           1             100
3           1             1000
4           1             200
4           2             100
5           1             1000

所以我需要,

结果

contract    instalment    amount
1           2             1000
2           1             100
3           1             1000
4           1             200
5           1             1000 

我对 linq 的了解有限,而且我在项目方面遇到了困难。

有任何想法吗?

4

2 回答 2

4

以下代码应为您提供预期的结果:

var data = new[]
{
    new { contract = 1, installment =  1, amount = 100},
    new { contract = 1, installment =  2, amount = 1000},
    new { contract = 2, installment =  1, amount = 100},
    new { contract = 3, installment =  1, amount = 1000},
    new { contract = 4, installment =  1, amount = 200},
    new { contract = 4, installment =  2, amount = 100},
    new { contract = 5, installment =  1, amount = 1000},
};

var result = from d in data
             group d by d.contract into g
             let highestInstallment = (from x in g
                                       orderby x.amount descending
                                       select x).First()
             select new
             {
                 contract = g.Key,
                 installment = highestInstallment.installment,
                 amount = highestInstallment.amount
             };   

您分组contract,然后在每个组中检查最高金额。我添加了该let子句,因此您不必对installmentand重复子查询amount

于 2012-08-14T10:22:55.820 回答
1

为那些寻找极简示例的人修改接受的答案:

var data = new[]
{
    new { contract = 1, installment =  1, amount = 100},
    new { contract = 1, installment =  2, amount = 1000},
    new { contract = 2, installment =  1, amount = 100},
    new { contract = 3, installment =  1, amount = 1000},
    new { contract = 4, installment =  1, amount = 200},
    new { contract = 4, installment =  2, amount = 100},
    new { contract = 5, installment =  1, amount = 1000},
};

这给出了相同的结果(不需要额外的选择):

var result1 = from d in data
            group d by d.contract
            into g
            select (from x in g
                    orderby x.amount descending
                    select x).First();

Linq方法链写的一样(一行代码):

var result2 = data.GroupBy(d => d.contract).Select(g => g.OrderByDescending(x => x.amount).First() ); 

将接受的答案重写为方法链(无论如何都有很多代码,但让它在这里)

var result3 = data.GroupBy(d => d.contract)
                .Select(g => new {g, highestInstallment = (g.OrderByDescending(x => x.amount)).First()})
                .Select(@t => new
                {
                    contract = @t.g.Key,
                    installment = @t.highestInstallment.installment,
                    amount = @t.highestInstallment.amount
                }); 
于 2016-12-28T15:10:39.650 回答