2

我想组合三个 LINQ 查询。每个查询都有不同的 where 条件,但 group by 和 select 子句始终遵循相同的模式。任何帮助是极大的赞赏!

查询一:

    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

2
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,
       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")),
       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")),
       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"))
   };
于 2013-06-14T09:56:43.517 回答
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 回答