I am currently facing an issue that debugging (VS2022) does not seem to solve for whatever reason.
In the below code, I am using the Gmail API package for .NET, where I'm able to extract out an email subject (string) and the email address (not included/relevant) from a given email.
var subject = m.Payload.Headers.Where(x => x.Name == "Subject").First().Value;
I then search via LINQ through a mapping table in my SQL database (EmailSubjectMappings) using EntityFramework Core to find the respective row that fulfills the below condition (if my self-entried "pattern" string is a substring of the main subject string)
var subjectMapping = context.EmailSubjectMappings.AsEnumerable().Where(x => (subject.Contains(x.EmailSubject))).FirstOrDefault();
In the case of subject containing no single quotes, this works fine. For example:
input
subject = "Fwd: Your Order was Shipped"
x.EmailSubject = "Your Order was Shipped"
Expected result: subjectMapping.EmailShippingStatus = "Order Delayed"
Actual result: subjectMapping.EmailShippingStatus = "Order Delayed"
However, in the case of single quotes:
input:
subject = "Fwd: Your Order Couldn't Be Delivered"
x.EmailSubject = "Your Order Couldn't Be Delivered"
Expected result: subjectMapping.EmailShippingStatus = "Order Not Delivered"
Actual result: subjectMapping.EmailShippingStatus = null
I went into the debugger, and checked each variable in the watch window to see what the root cause was:
context.EmailSubjectMappings.ToList().FirstOrDefault(x=> subject.Contains("Your Order Couldn't Be Delivered")) -> null (not expected)
context.EmailSubjectMappings.ToList().FirstOrDefault(x=>"Fwd: Your Order Couldn't Be Delivered".Contains(x.EmailSubject)) -> "Order Not Delivered" (expected)
context.EmailSubjectMappings.ToList().FirstOrDefault(x=>"Fwd: Your Order Couldn't Be Delivered".Contains("Your Order Couldn't Be Delivered")) -> "Order Not Delivered" (expected)
So through transition, it looks that the variable subject is the issue, and inputting that in the watch window shows also the expected string : "Fwd: Your Couldn't Be Delivered"
I've also tried removing all possible hidden characters using Regex
string output = Regex.Replace(subject, "[^\u0021-\u007E]", "");
and even using IndexOf() instead of .Contains.
So I'm stumped. Is there some sort of memory issue going on causing my subject variable to change? Is it some property of LINQ or EF Core? I appreciate any help!