Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何将LINQ查询与不同的where条件相结合_C#_Linq_Group By_Sum_Where - Fatal编程技术网

C# 如何将LINQ查询与不同的where条件相结合

C# 如何将LINQ查询与不同的where条件相结合,c#,linq,group-by,sum,where,C#,Linq,Group By,Sum,Where,我有三个LINQ querys我想合并。每个查询具有不同的where条件,但GROUPBY和select子句始终遵循相同的模式。非常感谢您的帮助 查询1: var querySales = from row in bookings.AsEnumerable() where (row.Field<Int32>("t-account") >= tAccSalesFrom && row.Field<Int

我有三个LINQ querys我想合并。每个查询具有不同的where条件,但GROUPBY和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"))
                         };
    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"))
                               };
    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"))
                            };
var querySales=来自bookings.AsEnumerable()中的行
其中(行字段(“t账户”)>=TACCSALEFROM和行字段(“t账户”)行字段(“销售资产”)-行字段(“销售借方”))
};
查询2:

    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"))
                         };
    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"))
                               };
    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"))
                            };
var queryLabourCosts=来自bookings.AsEnumerable()中的行
其中(行字段(“t账户”)>=tAccLabFrom和行字段(“t账户”)行字段(“销售借方”)-行字段(“销售资产”))
};
查询3:

    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"))
                         };
    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"))
                               };
    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"))
                            };
var queryMaterial=来自bookings.AsEnumerable()中的行
其中(行字段(“t账户”)>=tAccMatFrom和行字段(“t账户”)行字段(“销售借方”)-行字段(“销售资产”))
};
解决方案:多亏了懒散的别列佐夫斯基

                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"))
                               };
var querySalesLabMat=来自bookings.AsEnumerable()中的b
b组由纽约
{
b、 字段(“日期”)。年份,
b、 字段(“日期”)。月份,
}进入g
按g.Key.Year、g.Key.Month排序的订单
选择新的
{
g、 关键的一年,
g、 关键,一个月,
Sales=g.Where(r=>r.Field(“t-account”)>=tAccSalesFrom&r.Field(“t-account”)r.Field(“Sales\u资产”)-r.Field(“Sales\u借方”),
人工成本=g.Where(r=>r.Field(“t-account”)>=tAccLabFrom&r.Field(“t-account”)r.Field(“销售借方”)-r.Field(“销售资产”),
物料成本=g.Where(r=>r.Field(“t-account”)>=tAccMatFrom和r.Field(“t-account”)r.Field(“销售借方”)-r.Field(“销售资产”))
};
提前感谢您的帮助,
Mat

你是说:

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()
                         };
var querySales=来自bookings.AsEnumerable()中的行
其中(行字段(“t账户”)>=TACCSALEFROM和行字段(“t账户”)=tAccLabFrom和行字段(“t账户”)=tAccMatFrom和行字段(“t账户”)行字段(“销售资产”)-行字段(“销售借方”))
//按年度和月份分组,并根据t账户进行汇总
Sales=grp.Where().Sum(),
LabourCosts=grp.Where().Sum(),
MaterialCosts==grp.Where().Sum()
};

var querySalesLabMat=
从b开始预订。可计算()
b组由纽约
{
b、 字段(“日期”)。年份,
b、 字段(“日期”)。月份,
}进入g
按g.Key.Year、g.Key.Month排序的订单
选择新的
{
g、 关键的一年,
g、 关键,一个月,
人工成本=g.Where(r=>r.Field(“t-account”)>=tAccLabFrom&r.Field(“t-account”)r.Field(“销售借方”)-r.Field(“销售资产”),
Sales=g.Where(r=>r.Field(“t-account”)>=tAccSalesFrom&r.Field(“t-account”)r.Field(“Sales\u资产”)-r.Field(“Sales\u借方”),
物料成本=g.Where(r=>r.Field(“t-account”)>=tAccMatFrom和r.Field(“t-account”)r.Field(“销售借方”)-r.Field(“销售资产”))
};

看起来像是查找10个差异游戏:)很有可能,使用如此复杂的查询,您会无意中将计算逻辑从SQL server(我怀疑您使用了SQL server)移动到客户端计算机,这反过来会导致巨大的内存消耗和查询执行的明显延迟(直到SQL server中的所有表都将下载到您的计算机ram中)这并不是所有的SQL操作都在LINQ操作中有对应的,如果在复杂的表达式树的中间,至少有一个SQL模拟不存在——这将是强制编译器生成代码从SQL获取数据到本地内存并通过它运行查询操作符的触发器。@Sevenate这是LINQ到数据集-他已经有了所有的数据client@lazyberezovsky这样就有意义了,同意。这将只选择符合所有条件的行(销售、人工成本和材料成本)。但是有些行只与销售筛选器匹配,应该用于销售计算。非常感谢!我不知道为什么我无法将where条件设置为正确。我对答案进行了一点修改,解决了我的问题。