How to combine 2 linq statments with groupby clause into 1

Posted by AG. on Stack Overflow See other posts from Stack Overflow or by AG.
Published on 2010-03-15T16:24:28Z Indexed on 2010/03/15 22:39 UTC
Read the original article Hit count: 256

Filed under:
|

Hello Friends,

I was wondering if i can consolidate below 2 linq statments into 1 statment. I am sure it should be possible, but various attempts i am unable to manage.

var prevProvisionsBySubBook = (from provision in prevProvisions
               group provision by provision.SubBook
                   into subBookGrouping
                   select
                   new
                   {
                       Key = subBookGrouping.Key,
                       Value = subBookGrouping.Sum(t => t.ProvisionUSD)
                   });


        var currentProvisionsBySubBook
            = (from provision in currentProvisions
               group provision by provision.SubBook
                   into subBookGrouping
                   select new
                   {
                       Key = subBookGrouping.Key,
                       Value = subBookGrouping.Sum(t => t.ProvisionUSD)
                   });

        var adjustmentChangeBySubBook
            = (from current in currentProvisionsBySubBook
               select new
               {
                   Key = current.Key,
                   Value = current.Value
                           - (prevProvisionsBySubBook.Any() ? prevProvisionsBySubBook.Where(t => t.Key == current.Key).Single().Value : 0)
               });

any help would be apprecaited.

© Stack Overflow or respective owner

Related posts about c#

Related posts about LINQ