我必须创建一个 LINQ 查询加入 3 个 IEnumerable(2 个数据表,一个列表)。我还必须“外部连接”我的 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 秒内返回结果。
两者都有完全相同的结果数据。
有任何想法吗?