LINQ匿名类型 - 绕过Group By

时间:2014-11-12 15:24:45

标签: c# linq

我有LINQ sql(见下文,感谢Cameron)。我试图从类First获取属性(ItemCode)而不在Group by子句中使用它。 我该怎么做?

不要在group by中使用First.ItemCode,但仍希望它在First.Begin,First.End的输出中通过降序排序。

   public class First
    {
        public string Account;
        public DateTime Begin;
        public DateTime End;
        public decimal Amount;
        public string ItemCode;

    }

    public class Second
    {
        public string Account;
        public DateTime Begin;
        public DateTime End;
        public decimal Amount;
    }
                List<First> firstAccount = new List<First>();
                List<Second> secondAccount = new List<Second>();

                firstAccount.Add(new First()
                {
                    Account = "1234",
                    Begin = new DateTime(2014, 5, 13),
                    End = new DateTime(2014, 6, 12),
                    Amount = 9999,
                    ItemCode = "AAA"
                });

                firstAccount.Add(new First()
                {
                    Account = "1234",
                    Begin = new DateTime(2014, 6, 13),
                    End = new DateTime(2014, 7, 7),
                    Amount = 1000,
                    ItemCode = "AAA"
                });

                firstAccount.Add(new First()
                {
                    Account = "1234",
                    Begin = new DateTime(2014, 6, 13),
                    End = new DateTime(2014, 7, 14),
                    Amount = 0,
                    ItemCode = ""
                });

                firstAccount.Add(new First()
                {
                    Account = "1234",
                    Begin = new DateTime(2014, 7, 7),
                    End = new DateTime(2014, 7, 14),
                    Amount = 1000,
                    ItemCode = "BBB"
                });

                secondAccount.Add(new Second()
                {
                    Account = "1234",
                    Begin = new DateTime(2014, 5, 13),
                    End = new DateTime(2014, 6, 12),
                    Amount = 9999
                });

                secondAccount.Add(new Second()
                {
                    Account = "1234",
                    Begin = new DateTime(2014, 6, 13),
                    End = new DateTime(2014, 7, 14),
                    Amount = 2000
                });

var result = from account in (from first in firstAccount
                                join second in secondAccount
                                  on first.Account equals second.Account
                                where
                                  ((first.Begin >= second.Begin && first.Begin <= second.Begin) &&
                                   (first.End >= second.Begin && first.End <= second.End))
                                select new
                                {
                                  first.Account,
                                  second.Begin,
                                  second.End,
                                  first.Amount,
                                  first.ItemCode
                                })
               group account by new {account.Account, account.Begin, account.End }
               into groupedAccounts
               select new
               {
                 groupedAccounts.Key.Account,
                 groupedAccounts.Key.Begin,
                 groupedAccounts.Key.End,
                 Sum = groupedAccounts.Sum(a => a.Amount)
               };

1 个答案:

答案 0 :(得分:1)

获取itemcode的一种方法是更改​​上一个select。 添加此行

Itemcode = String.Join(" ",groupedAccounts.Select(q=> q.ItemCode))
Sum = groupedAccounts.Sum(a => a.Amount),

之后

它应该产生itemcode

foreach (var data in result)
{
   Console.WriteLine(data.Account + " " + data.Itemcode);                
}

输出

1234 AAA
1234 AAA