Use string. Equals: from p in People.All() where (firstname == null || string. Equals (firstname, p.
FirstName, StringComparison. InvariantCultureIgnoreCase)) && (lastname == null || string. Equals (lastname, p.
LastName, StringComparison. InvariantCultureIgnoreCase)) select p Not only does this avoid the null problem, but it forces you to specify a string comparison type (a good thing). In other words, you specify whether to use rules specific to the local or invariant culture when performing the case-insensitive comparison.
Just curious... – Josh E Aug 18 '10 at 16:32.
You don't need to call ToLower() use string. Compare instead with ignoreCase string. Compare(firstName, p.
FirstName, true).
Make sure it's not null before calling the ToLower: (firstname==null || (firstname! = null && firstname.ToLower()==p.FirstName.ToLower())).
It doesn't work when both are null because the first part of the statement will evaluate to true, disallowing short-circuiting of the evaluation. Why not use equivalency rules to flip the statement? (firstName!
= null && firstName.ToLower() == p.firstName.ToLower()) EDIT: I wrote the following up and ran it successfully in LINQPad 4, with no issues. I'm assuming that the call to People.All() simply returns an IQueryable with the full set of records? Maybe post your exception text here so we can see if there's something you've inadvertently missed?
Void Main() { string a = null; string be = null; var peeps = new List { new Person { FirstName = "John", LastName = "Connor" }, new Person { FirstName = "Sarah", LastName = "Connor", }, new Person { FirstName = "Cletus", LastName = "Handy" } }; var somePeeps = from p in peeps where (a == null || a.ToLower() == p.FirstName.ToLower()) && (b == null || b.ToLower() == p.LastName.ToLower()) select p; somePeeps.Dump(); } public class Person { public string FirstName { get; set;} public string LastName { get; set;} }.
If the first part is true, then it shouldn't evaluate the second part due to short-circuited OR though. – Earlz Jul 16 '10 at 18:07.
I have a page where you can search for people. They can either get a list of all people, or filter it by certain criteria such as first or last name. So far, I have been trying trying to use the technique detailed in this question.
I get a null reference error when building the query however when both firstname and lastname is null. Removing the where clause gets rid of the error. Why would this not work?
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.