我必须创建一个连接3 IEnumerable(2个数据表,一个列表)的LINQ查询.我还必须“外部加入”我的2个加入.
我真的很喜欢第二个,因为它更易读并且可与SQL媲美.但是与第一个相比,我遇到了性能问题(长3倍),可读性较差,但速度更快.
有人可以向我解释如何优化第二个查询,以及为什么它们有很大不同,但是返回相同的数据(我对LINQ陌生,并且很难理解每个LINQ-TO-SQL / LAMBDA / SAMBA实用程序
旧的,真的很快,但是不太容易听
var Detailled_payroll =
from MainData in dt_BasicPayrollData.AsEnumerable()
//joining (Externalize later...) with a list of days
join MyDays in gdt_Liste_JourFérié
on MainData.Field<DateTime>("DENTREE") equals MyDays
into MyDays_join //why?
//joining (Externalize later...) with a list (datatable) of things
join MyFilter in gdt_MyFilter.AsEnumerable()
on MainData.Field<string>("KEY") equals MyFilter.Field<string>("KEY")
into MyFilter_join //why?
//I think we "outer join" here, forcing the existing of a missing join
from MyDays in MyDays_join.DefaultIfEmpty()
from MyFilter in MyFilter_join.DefaultIfEmpty()
let tmp = MainData.Field<string>("CEmploye") //dont remember why..
select new Blabla()
{
EmpNumber=MainData.Field<string>("NoEmploye"),
IsInSpecialDays = MyDays == Default(DateTime) ? "NO" : "YES",
KeyWasFound = MyFilter == null ? "NO" : "YES"
}
这是“新”的一个:
var Detailled_payroll =
from MainData in dt_BasicPayrollData.AsEnumerable()
//joining (and externalize...) with a list (datatable) of things
from MyFilter in MyGlobalVariable.gdt_FiltreEnCours.AsEnumerable()
.Where(x => x.Field<string>("KEY") == MainData.Field<string>("KEY"))
.DefaultIfEmpty()
//joining (and externalize...) with a list (List<DateTime>) of days
from MyDays in gdt_Liste_JourFérié
.Where(x => x == MainData.Field<DateTime>("DENTREE"))
.DefaultIfEmpty()
select new Blabla()
{
EmpNumber=MainData.Field<string>("NoEmploye"),
IsInSpecialDays = MyDays == Default(DateTime) ? "NO" : "YES",
KeyWasFound = MyFilter == null ? "NO" : "YES"
}
>“旧”查询将在3秒钟内返回结果.
>“新”查询将在10秒内返回结果.
两者具有完全相同的结果数据.
有任何想法吗?
解决方法:
简而言之,在第一个中您具有联接,在第二个中您具有笛卡尔积,然后对其进行过滤.