I have a nullable field in a database that is populated if users enter an optional value. In this case it is for keywords which used to be required whenever a user created a post, but to make things more user friendly I recently changed it to an optional field. That has resulted in a Lambda expression I use to query that field breaking.
The expression gets every value from that field from the posts table and iterates through it to create keyword search suggestions. It worked fine until the first user decided not to fill out the keywords field on the form. Now instead of simply failing to match null rows with the search term it breaks the form.
Here is the expression:
public async Task<string[]> GetAutoComplete(string search, int count)
{
string[] tags = (await GetPosts()).Select((Posts t) => t.Tags).ToArray();
List<string> suggestions = new List<string>();
string[] array = tags;
foreach (string tag in array)
{
Array tagwords = tag.Split(",");
foreach (object? tagword in tagwords)
{
suggestions.Add(tagword!.ToString()!.ToLower().Trim());
}
}
IEnumerable<string> finalsuggestions = from o in suggestions.Distinct()
orderby o
select o into m
where m.StartsWith(search, StringComparison.CurrentCultureIgnoreCase)
select m;
return finalsuggestions.Take(count).ToArray();
}How do I change this so that a result where no match exists due to the corresponding value in the database being null is treated the same as if it were an empty string?
Right now I get an object not set to an instance of an object error due to some of the values being null