我有2个数据表:
前一天数据:
ID Account IsEmail EmailAddress
--------------------------------------------
1 1111 N [email protected] // Invalid email
2 1112 Y [email protected] // Valid email
3 1113 Y [email protected] // Valid email
当日数据:
ID Account IsEmail EmailAddress
--------------------------------------------
1 1111 Y [email protected] // Valid email value changed
2 1112 Y [email protected] // Valid email
3 1113 Y [email protected] // Valid email value changed
4 1114 N NULL // Invalid email
I am doing a left join `CurrentDay` with `PreviousDay`.
Conditions:
1. "ID" and "Account" of `CurrentDay` must match with "ID" and "Account" of `PreviousDay`
2. "IsEmail" OR "EmailAddress" value has changed from `PreviousDay`
This query returns the result data:
ID Account IsEmail EmailAddress
-------------------------------------------
1 1111 Y [email protected] // Valid email
3 1113 Y [email protected] // Valid email
4 1114 N NULL // Invalid email
Query:
var ResultData = (from rowCurrent in currentDay.AsEnumerable()
join rowPrevious in previousDay.AsEnumerable()
on new { ID = rowCurrent.Field<string>("ID"), Account = rowCurrent.Field<string>("Account") }
equals new { ID = rowPrevious .Field<string>("ID"), Account = rowPrevious .Field<string>("Account") }
into rowRight
from rowJoin in rowRight.DefaultIfEmpty()
where
(rowCurrent.Field<string>("IsEmail") != rowJoin?.Field<string>("IsEmail")) ||
(rowCurrent.Field<string>("EmailAddress") != rowJoin?.Field<string>("EmailAddress"))
select new ResultData
{
ID = rowCurrent.Field<string>("ID"),
Account = rowCurrent.Field<string>("Account"),
IsEmail = rowCurrent.Field<string>("IsEmail"),
EmailAddress = rowCurrent.Field<string>("EmailAddress")
}
).ToList();
问题:如果 CurrentDay 中的新记录无效,如何从 ResultData 中仅删除该记录。
ID Account IsEmail EmailAddress
4 1114 N NULL // Invalid email
如果 CurrentDay 中有一条新记录在 PreviousDay 中不存在,我需要检查“IsEmail”!=“Y”或“EmailAddress”不是有效的电子邮件,那么我不想包含在 ResultData 中。
Note: I have a helper method to check Valid Email address string.
我尝试了以下左连接查询:
var ResultData = (from rowCurrent in currentDay.AsEnumerable()
join rowPrevious in previousDay.AsEnumerable()
on new { ID = rowCurrent.Field<string>("ID"), Account = rowCurrent.Field<string>("Account") }
equals new { ID = rowPrevious .Field<string>("ID"), Account = rowPrevious .Field<string>("Account") }
into rowRight
from rowJoin in rowRight.DefaultIfEmpty()
where
rowJoin == null ?
(
(rowCurrent.Field<string>("IsEmail") == "Y") &&
(IsValidEmail(rowCurrent.Field<string>("EmailAddress")))
) :
(
(rowCurrent.Field<string>("IsEmail") != rowJoin?.Field<string>("IsEmail")) ||
(rowCurrent.Field<string>("EmailAddress") != rowJoin?.Field<string>("EmailAddress"))
)
select new ResultData
{
ID = rowCurrent.Field<string>("ID"),
Account = rowCurrent.Field<string>("Account"),
IsEmail = rowCurrent.Field<string>("IsEmail"),
EmailAddress = rowCurrent.Field<string>("EmailAddress")
}
).ToList();
否则: