简体   繁体   中英

C# LINQ leftjoin remove Invalid record

I have 2 Datatables:

PreviousDayData

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

CurrentDayData

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

Question: How do I remove only the new record in CurrentDay from ResultData if it is not valid.

ID  Account  IsEmail  EmailAddress
4   1114     N        NULL                   // Invalid email

If there is a new record in CurrentDay that is not present in PreviousDay, I need to check "IsEmail",= "Y" OR "EmailAddress" is not a validEmail. then I do not want to include in the ResultData.

Note: I have a helper method to check Valid Email address string.

I tried the following query for left join:

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

-- If a non-matching row is found, it will select only if it is a Valid Record.

OR ELSE

-- If a matching row is found, it will select if "IsEmail" OR "EmailAddress" value has changed from PreviousDay

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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