Replacing null in a list


Please Share

.net coreI was just working on an issue here. For some reason when we post data to the server the language is wrong sometimes we have users with data that is half English and half Danish. Its been very hard to track down this issue. After digging around in the logging a little I found something interesting


     {  
         "Mail":"xxxxx@gmail.com",
         "Name":"Jane doe",
         "Culture":"null",
         "Role":"SalesAssistent"
      }

Apparently the users are submitting to null language to the API. I am not really in control of what is being sent to this API but i am in control of the API itself. So after a bit of discussion with my coworkers we have decided if you don’t send a language we are going to default it to da-DK

So i thought well i can just do a check and replace it. THere is actually a list of users that are being sent so I was going to have to loop though them all and replace them one at a time. Well i hate doing that. So lets do it with Linq.


var x = users.Select(c =>
    {
    c.Culture = string.IsNullOrWhiteSpace(c.Culture) ? "da-DK" : c.Culture;
    return c;
    }).ToList();

I found this solution worked well for me. It just checks if the culture is null or space and then replaces it. This should ensure that things are at least defaulting to Danish.

Please Share


Linda Lawton

About Linda Lawton

My name is Linda Lawton I have more than 20 years experience working as an application developer and a database expert. I have also been working with Google APIs since 2012 and I have been contributing to the Google .Net client library since 2013. In 2013 I became a a Google Developer Experts for Google Analytics.

Leave a comment

Your email address will not be published. Required fields are marked *