2

結合したい 3 つの LINQ クエリがあります。各クエリには異なる where 条件がありますが、group by 句と select 句は常に同じパターンに従います。どんな助けでも大歓迎です!

クエリ 1:

    var querySales = from row in bookings.AsEnumerable()
                         where (row.Field<Int32>("t-account") >= tAccSalesFrom && row.Field<Int32>("t-account") <= tAccSalesTo)
                         group row by new
                         {
                             year = row.Field<DateTime>("Date").Year,
                             month = row.Field<DateTime>("Date").Month
                         } into grp
                         orderby grp.Key.year, grp.Key.month
                         select new
                         {
                             Year = grp.Key.year,
                             Month = grp.Key.month,
                             Sales = grp.Sum(row => row.Field<Decimal>("Sales_Assets") - row.Field<Decimal>("Sales_Debit"))
                         };

クエリ 2:

    var queryLabourCosts = from row in bookings.AsEnumerable()
                               where (row.Field<Int32>("t-account") >= tAccLabFrom && row.Field<Int32>("t-account") <= tAccLabTo)
                               group row by new
                               {
                                   year = row.Field<DateTime>("Date").Year,
                                   month = row.Field<DateTime>("Date").Month
                               } into grp
                               orderby grp.Key.year, grp.Key.month
                               select new
                               {
                                   Year = grp.Key.year,
                                   Month = grp.Key.month,
                                   LabourCosts = grp.Sum(row => row.Field<Decimal>("Sales_Debit") - row.Field<Decimal>("Sales_Assets"))
                               };

クエリ 3:

    var queryMaterial = from row in bookings.AsEnumerable()
                            where (row.Field<Int32>("t-account") >= tAccMatFrom && row.Field<Int32>("t-account") <= tAccMatTo)
                            group row by new
                            {
                                year = row.Field<DateTime>("Date").Year,
                                month = row.Field<DateTime>("Date").Month
                            } into grp
                            orderby grp.Key.year, grp.Key.month
                            select new
                            {
                                Year = grp.Key.year,
                                Month = grp.Key.month,
                                MaterialCosts = grp.Sum(row => row.Field<Decimal>("Sales_Debit") - row.Field<Decimal>("Sales_Assets"))
                            };

解決策: lazyberezovsky に感謝します!

                var querySalesLabMat = from b in bookings.AsEnumerable()
                               group b by new
                               {
                                   b.Field<DateTime>("Date").Year,
                                   b.Field<DateTime>("Date").Month,
                               } into g
                               orderby g.Key.Year, g.Key.Month
                               select new
                               {
                                   g.Key.Year,
                                   g.Key.Month,
                                   Sales = g.Where(r => r.Field<Int32>("t-account") >= tAccSalesFrom && r.Field<Int32>("t-account") <= tAccSalesTo)
                                                    .Sum(r => r.Field<Decimal>("Sales_Assets") - r.Field<Decimal>("Sales_Debit")),
                                   LabourCosts = g.Where(r => r.Field<Int32>("t-account") >= tAccLabFrom && r.Field<Int32>("t-account") <= tAccLabTo)
                                                  .Sum(r => r.Field<Decimal>("Sales_Debit") - r.Field<Decimal>("Sales_Assets")),
                                   MaterialCosts = g.Where(r => r.Field<Int32>("t-account") >= tAccMatFrom && r.Field<Int32>("t-account") <= tAccMatTo)
                                                  .Sum(r => r.Field<Decimal>("Sales_Debit") - r.Field<Decimal>("Sales_Assets"))
                               };

助けてくれてありがとう、マット

4

2 に答える 2

0

次のような意味ですか。

var querySales = from row in bookings.AsEnumerable()
                         where (row.Field<Int32>("t-account") >= tAccSalesFrom && row.Field<Int32>("t-account") <= tAccSalesTo)
                         where (row.Field<Int32>("t-account") >= tAccLabFrom && row.Field<Int32>("t-account") <= tAccLabTo)
                         where (row.Field<Int32>("t-account") >= tAccMatFrom && row.Field<Int32>("t-account") <= tAccMatTo)
                         group row by new
                         {
                             year = row.Field<DateTime>("Date").Year,
                             month = row.Field<DateTime>("Date").Month
                         } into grp
                         orderby grp.Key.year, grp.Key.month
                         select new
                         {
                             Year = grp.Key.year,
                             Month = grp.Key.month,
                             Sales = grp.Sum(row => row.Field<Decimal>("Sales_Assets") - row.Field<Decimal>("Sales_Debit"))

                             //Group by year and month and sum based on the t-account
                               Sales = grp.Where().Sum(),
                               LabourCosts = grp.Where().Sum(),
                               MaterialCosts = = grp.Where().Sum()
                         };

?

于 2013-06-14T09:44:34.683 に答える