2

在解决这个问题时遇到了一些麻烦,我有一个我在 LINQ 中按帐户分组的交易列表,现在它给了我一个所有帐户的可枚举,每个帐户都有一个可枚举的交易。我现在想获取该列表,并按每个组的事务总和对其进行排序,但它似乎不起作用,这是我到目前为止所拥有的:

var memberTrans = trans.GroupBy(o => o.ShareAccountId);
var transOrdered = memberTrans.OrderByDescending(o => o.Sum(i => i.TransactionAmount));

我的想法是,我可以做 .First() 并获得在此期间净增长最高的帐户,并 .Last() 获得净增长最低的帐户。

假设我有如下数据(简化)

  • 账户 1:300
  • 账户 1:-300
  • 账户 2:100
  • 账户 3 : 50
  • 账户 2:-20

所以第一个分组给了我:

账户 1:

  • 转:300
  • 转:-300

账户 2:

  • 转:100
  • 转:-20

账户 3:

  • 转:50

现在我想按每个组内交易的总和对其进行排序,所以我们有一个列表:

账户 2:

  • 转:100
  • 转:-20

账户 3:

  • 转:50

账户 1:

  • 转:300
  • 转:-300

希望这足够清楚,非常感谢任何帮助!

编辑:为了清楚起见,更多代码:

var tranOrder = trans.GroupBy(o => o.ShareAccountId).OrderByDescending(g => g.Sum(t => t.TransactionAmount));

        var bigUp = tranOrder.First().First();
        var bigDown = tranOrder.Last().First(); 
        report.BiggestMovementUp = tranOrder.First().Sum(o => o.TransactionAmount);
        report.BiggestMoverUp =
            (bigUp.Member.Title + " " + bigUp.Member.Forename + " " + bigUp.Member.Surname + " - ID " + bigUp.Member.MemberID).Trim();
        report.BiggestMovementDown = tranOrder.Last().Sum(o => o.TransactionAmount);
        report.BiggestMoverDown =
          (bigDown.Member.Title + " " + bigDown.Member.Forename + " " + bigDown.Member.Surname + " - ID " + bigDown.Member.MemberID).Trim();

4

3 回答 3

1
  var v = (from acc in accs
           group acc by acc.ShareAccountId into g
           orderby g.Sum(x => x.TransactionAmount) descending
           select g.Sum(x => x.TransactionAmount));

应该这样做,您可以在选择中做任何您想做的事情。我也测试过这个。

于 2012-11-22T13:52:34.283 回答
0

这个怎么样:

internal class Program
    {
        private class AccountOperation
        {
            public int Id { get; set; }
            public int Amount { get; set; }
        }

        private static void Main(string[] args)
        {
            List<AccountOperation> ops = new List<AccountOperation>
            {
                new AccountOperation { Id = 1, Amount = 300},
                new AccountOperation { Id = 1, Amount = -300},
                new AccountOperation { Id = 2, Amount = 100},
                new AccountOperation { Id = 3, Amount = 50},
                new AccountOperation { Id = 2, Amount = -20},
            };

            foreach (var operationsByAccount in ops
                .OrderByDescending(z => z.Amount)  // Orders all amounts descending
                .GroupBy(z => z.Id) // Groups by account ID
                .OrderByDescending(z => z.Sum(z2 => z2.Amount))) // Orders groups of operations per accounts descending (by sum of amounts)
            {
                Console.WriteLine("Operation " + operationsByAccount.Key);
                foreach (var operation in operationsByAccount)
                {
                    Console.WriteLine("\tAmout " + operation.Amount);
                }
            }

            Console.ReadLine();
        }
    }

输出:

  • 操作 2
    • 大约 100
    • 金额 -20
  • 操作 3
    • 大约 50
  • 操作 1
    • 大约 300
    • 金额 -300
于 2012-11-22T14:00:39.833 回答
0

对于您的方法链接版本的修复:

var v = accs.GroupBy(acc => acc.ShareAccountId)
        .OrderByDescending(g => g.Sum(x => x.TransactionAmount))
        .Select( g => g.Sum(x => x.TransactionAmount));

诚然,我让 Resharper 从 LINQ 语法中为我做这件事 :)

于 2012-11-22T15:33:24.923 回答