santosh singh
santosh singh

Reputation: 28652

Handling NULL Parameters in LINQ Queries

Let's say you have parameters(s) in your LINQ query where clause, how do you handle that?

Here is an example:

var peoples= from i in individuals
  where (string.IsNullOrEmpty(lastName) i.LastName.Equals(lastName))
  select i;

Upvotes: 3

Views: 2188

Answers (5)

VVS
VVS

Reputation: 19604

You do it in LINQ like you would do in "normal" C#.

In your sample you used string.IsNullOrEmpty(), so you don't differentiate between null and "". If that's the case you could write sth. like the following:

if ((lastName ?? "") == (i.LastName ?? ""))
{
   // equal
}

Note: The ?? operator returns the left-hand operand if it is not null, or else it returns the right operand.

You can integrate that logic into your LINQ query as follows:

var peoples = from i in individuals
              where (lastName ?? "") == (i.LastName ?? "")
              select i;

Upvotes: 2

Jon Skeet
Jon Skeet

Reputation: 1500425

Try to understand what's going on under the hood:

  • How query expressions are translated by the compiler
  • How LINQ to Objects streams its data, and how it defers execution
  • How queries execute remotely via IQueryable and expression trees

It's also worth becoming comfortable with both query expressions, e.g.

var query = from person in people
            where person.IsAdult
            select person.Name;

and "dot notation":

var query = people.Where(person => person.IsAdult)
                  .Select(person => person.Name);

Knowing both will let you choose the most readable form for any particular query.

Upvotes: 15

Sorax
Sorax

Reputation: 2203

The best advice I could offer someone just starting with LINQ is get LINQPad. It is an invaluable tool and will speed up your learning process significantly. And it comes with lots and lots of samples that demonstrate, and let you work with, all the LINQ operators.

Upvotes: 2

Eric Lippert
Eric Lippert

Reputation: 660032

The result of a query expression is a query object, not the results of the query. If you want the results, you can ask the query object to start proffering up results. That is, when you say:

var names = from c in customers where c.City == "London" select c.Name;

then names is a query object that represents "get me the names of all the customers in London". It is not a sequence in memory of all the names of customers who live in London; the results of the query are computed on-demand. It is not until you say:

foreach(var name in names) ...

that the actual results are computed.

This means that if you ask the same query object for its results twice, the answer might be different. The customers list might have been changed between the first time you ask and the second time you ask. Because queries defer getting their results, you always get fresh results. But you sometimes do the same work twice.

Upvotes: 10

Serkan Hekimoglu
Serkan Hekimoglu

Reputation: 4284

101 LinQ Samples you can start with samples from MSDN

Upvotes: 5

Related Questions