繁体   English   中英

C# LINQ leftjoin 删除无效记录

[英]C# LINQ leftjoin remove Invalid record

我有 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

当前日期数据

ID  Account  IsEmail  EmailAddress
1   1111     Y        abc1Changed@gmail.com  // Valid email value changed
2   1112     Y        abc2@gmail.com         // Valid email
3   1113     Y        abc3Changed@gmail.com  // Valid email value changed
4   1114     N        NULL                   // Invalid email
I am doing leftjoin 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

The below query returns ResultData:

    ID  Account  IsEmail  EmailAddress
    1   1111     Y        abc1Changed@gmail.com  // Valid email
    3   1113     Y        abc3Changed@gmail.com  // Valid email
    4   1114     N        NULL                   // Invalid email

    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
                (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();

问题:如果无效,如何从 ResultData 中仅删除 CurrentDay 中的新记录。

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.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();

-- 如果找到一个不匹配的行,只有当它是一个有效记录时它才会是 select。

要不然

-- 如果找到匹配的行,如果“IsEmail”或“EmailAddress”的值已从 PreviousDay 更改,它将是 select

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM