I have a table with a column that I need to change it's values before comparing with a .Where()
.
Take this example: if the value in db is 'My sTriNg', I want to transform to 'my_string' before comparing.
Is there a lightweight algorythm to do this?
UPDATE: So after the answer I realized that I can do this:
public static string RemoveAccentsAndSpaceFromField(this string sNomenclature)
{
StringBuilder sbReturn = new StringBuilder();
var arrayText = sNomenclature.ToLowerInvariant().Normalize(NormalizationForm.FormD).ToCharArray();
foreach (char letter in arrayText)
{
if (CharUnicodeInfo.GetUnicodeCategory(letter) != UnicodeCategory.NonSpacingMark)
sbReturn.Append(letter);
}
string sNoAccents = sbReturn.ToString();
string sNoSpaces = sNoAccents.Replace(" ", "_");
return sNoSpaces;
}
Then use it like this:
using (var oContext = new MyContext())
{
bool bFieldExists = oContext.Fields.Any(s => s.Nomenclature.RemoveAccentsAndSpaceFromField() == sFieldName && s.id != iFieldId);
}
CodePudding user response:
No need to transform the actual value. You can do this.
string StringToCompare = "my_string";
var FilteredList = DbContext.TableName
.Where(i => i.ColumnName.ToLower().Replace(' ', '_') == StringToCompare.ToLower())
.ToList();