8

I am working on a project where I use Linq for fetching data. Now I have a scenario where I have to check field value contains given string for that I use Contains() function.

Everything works fine, But when any of the field is null then it creates a problem.

personData = personData.Where(x => x.FirstName.ToLower().Contains(q.ToLower()) || x.LastName.ToLower().Contains(q.ToLower())).Select(x => x).ToList();

Here when FirstName or LastName field have a null value it throw an error.

So, how can I overcome from this problem ?

5
  • 4
    Add a clause checking if x.FirstName is null..? Commented Jan 6, 2017 at 11:37
  • what version of c# are you using? if c#6 then null-conditional is an option. Commented Jan 6, 2017 at 11:42
  • I am using C# 4.5 @Nkosi Commented Jan 6, 2017 at 11:53
  • What if q is null? How do you handle that? Commented Sep 4, 2019 at 18:41
  • @stomy, q is parameter to function so we can first check if(string.IsNullOrEmpty(q)) {} Commented Sep 17, 2019 at 6:16

5 Answers 5

8

Please try this

personData = personData.Where(x => (x.FirstName != null && x.FirstName.ToLower().Contains(q.ToLower())) ||  (x.LastName != null && x.LastName.ToLower().Contains(q.ToLower()))).Select(x => x).ToList();
Sign up to request clarification or add additional context in comments.

3 Comments

Great to help you. :)
use named method instead of long lambda. private static bool Filter(string input, string search) {....} and then use it in Where. personData.Where(x => Filter(x, q)) @Jigarb1992
please post an answer @M.kazemAkhgary
7

Use following approach: x.FirstName?.Contains(substring) ?? false

Since C# 6 you can use null-conditional operators, that simplifies some queries greatly. You can read more about this topic here

Comments

4

You must check first if required values are null, Try using the null-coalescing operator...

personData = personData.Where(x => ((x.FirstName.ToLower() ?? "").Contains(q.ToLower())) ||  ((x.LastName.ToLower() ?? "").Contains(q.ToLower()))).Select(x => x).ToList();

2 Comments

It throw error "Operator '&&' cannot be applied to operands of type 'string' and 'bool'" @RahulHendawe
Great! it is an better way to handle such situation where if you get any null value then those values were replaced by _blank value :)
1

What about using a simple string extension like the following:

public static string AsNotNull(this string value)
{
    if (string.IsNullOrWhiteSpace(value))
        return string.Empty;

    return value;
}

And then use it like:

x.FirstName.AsNotNull()

1 Comment

Likewise, to avoid null when you expect a list, you can add a 2nd extension as overload from this answer. Then, use it in the same way, i.e. .Select(x => x).AsNotNull().ToList();. With both extensions, one for strings, one for enumerables, you are well armed!
-1

I think you should prevent ability to add null FirstName or LastName at the beggining. This kind of row seems unuseful.

2 Comments

this is good suggestion. because if both FirstName and LastName could be null then whats the point of having names? I suggest put "" instead of null if FirstName or LastName has to be empty
the code I have written in my question is just for example in actual there may be any field/s.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.