C# LINQ左连接移除无效记录

x33g5p2x  于 2022-12-15  发布在  C#
关注(0)|答案(1)|浏览(175)

我有2个数据表:
前一天数据

ID  Account  IsEmail  EmailAddress
1   1111     N        abc1Invalid@gmail.com  // Invalid email
2   1112     Y        abc2@gmail.com         // Valid email 
3   1113     Y        abc3@gmail.com         // Valid email

当日数据
x一个一个一个一个x一个一个二个x
问:如果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.
o2rvlv0m

o2rvlv0m1#

我尝试了以下左联接查询:

var ResultData = (from rowCurrent in currentDay.AsEnumerable()
            join rowPrevious in previousDay.AsEnumberable()
            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();

--如果发现不匹配的行,则仅当它是有效记录时才选择。
或其他
--如果找到匹配行,则将选择“IsEmail”或“EmailAddress”值是否已从PreviousDay更改

相关问题