Processing NULL Parameters in LINQ Queries

Say you have parameters in your LINQ query where is the sentence, how do you deal with this?

Here is an example:

var peoples= from i in individuals
  where (string.IsNullOrEmpty(lastName) i.LastName.Equals(lastName))
  select i;
+3
source share
5 answers

Try to understand what is happening under the hood:

  • How are query expressions translated by the compiler
  • How LINQ to Objects passes its data and how it cancels execution.
  • How queries are executed remotely through IQueryable trees and expressions

It is also useful to arrange both query expressions, for example

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 allow you to choose the most readable form for any particular request.

+15
source

- , . , . , :

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

- , " ". , ; . :

foreach(var name in names) ...

.

, , . , , , , , . , . .

+10
+5

, -, LINQ, - LINQPad. . , LINQ.

+2

LINQ, #.

string.IsNullOrEmpty(), null "". , sth. :

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

: The?? , , .

LINQ :

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

Source: https://habr.com/ru/post/1776475/


All Articles