null-coalescing operator with wildcard in a lambda expression

tintyethan

I have a search form with multiple inputs - first name, last name, company...

I would like to return entities based on user inputs, or if nothing is input, in the last name field for instance, return all last names.

I believe I should be using null-coalescing for this, just like "ISNULL" in t-sql...

contacts = contacts.Where(s => s.firstname.ToUpper().Contains(fNameSearch.ToUpper() ?? *)

The issue is that I don't know how to use a wildcard in this type of experession.

For instance, this returns everything that contains "test" in the firstname property if fNameSearch is null or white-space...

 contacts = contacts.Where(s => s.firstname.ToUpper().Contains(fNameSearch.ToUpper() ?? "test")

but I want to be able to return everything, not just "test".

Douglas

If you want to return everything, can't you place the condition before the query?

var contacts = /* construct your original query here */
if (fNameSearch != null)
    contacts = contacts.Where(s => s.firstname.ToUpper().Contains(fNameSearch.ToUpper());

Edit: Per Steve's comment below, case-sensitivity depends on the database's collation (and is case-insensitive by default), so you can just run:

var contacts = /* construct your original query here */
if (fNameSearch != null)
    contacts = contacts.Where(s => s.firstname.Contains(fNameSearch));

Edit2: If you have multiple inputs to check for, you can use the same approach compositionally:

var contacts = /* construct your original query here */
if (string.IsNullOrEmpty(fNameSearch) == false))
    contacts = contacts.Where(s => s.firstname.Contains(fNameSearch));
if (string.IsNullOrEmpty(fSurnameSearch) == false))
    contacts = contacts.Where(s => s.surname.Contains(fSurnameSearch));
if (string.IsNullOrEmpty(fCompanySearch) == false))
    contacts = contacts.Where(s => s.company.Contains(fCompanySearch));

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Performance of expression behind null coalescing operator

From Java

Is there a "null coalescing" operator in JavaScript?

From Dev

Null coalescing operator override

From Dev

Null coalescing operator with bool

From Dev

Null coalescing operator and casting with as

From Dev

Null coalescing operator override

From Java

PHP Null coalescing operator usage

From Dev

Null Coalescing Operator in F#?

From Dev

null coalescing operator assignment to self

From Dev

null-coalescing operator in the getter

From Dev

?? (null coalescing) vs ? (ternary if) expression

From Java

PHP ternary operator vs null coalescing operator

From Dev

Usage of ?? operator (null-coalescing operator)

From Dev

Usage of ?? operator (null-coalescing operator)

From Dev

sql null in c# and null coalescing operator

From Dev

null-coalescing operator on a property get

From Dev

Implicit conversion with null-coalescing operator

From Java

What is null coalescing assignment ??= operator in PHP 7.4

From Dev

Is there an equal for the c# null coalescing operator in java?

From Dev

Is there a way to implement and make use of a "NOT null coalescing" operator?

From Dev

Is there an equivalent of isset or a null coalescing operator in SASS?

From Dev

Is there a way to implement and make use of a "NOT null coalescing" operator?

From Dev

Is there an equivalent of isset or a null coalescing operator in SASS?

From Dev

How to define a null-coalescing operator for Twig?

From Java

an expression tree lambda may not contain a null propagating operator

From Dev

Lambda Expression using OR operator

From Dev

Null-coalescing operator returning null for properties of dynamic objects

From Dev

Null propagation + null coalescing operator not working as expected when dealing with InnerException

From Dev

Null propagation + null coalescing operator not working as expected when dealing with InnerException

Related Related

  1. 1

    Performance of expression behind null coalescing operator

  2. 2

    Is there a "null coalescing" operator in JavaScript?

  3. 3

    Null coalescing operator override

  4. 4

    Null coalescing operator with bool

  5. 5

    Null coalescing operator and casting with as

  6. 6

    Null coalescing operator override

  7. 7

    PHP Null coalescing operator usage

  8. 8

    Null Coalescing Operator in F#?

  9. 9

    null coalescing operator assignment to self

  10. 10

    null-coalescing operator in the getter

  11. 11

    ?? (null coalescing) vs ? (ternary if) expression

  12. 12

    PHP ternary operator vs null coalescing operator

  13. 13

    Usage of ?? operator (null-coalescing operator)

  14. 14

    Usage of ?? operator (null-coalescing operator)

  15. 15

    sql null in c# and null coalescing operator

  16. 16

    null-coalescing operator on a property get

  17. 17

    Implicit conversion with null-coalescing operator

  18. 18

    What is null coalescing assignment ??= operator in PHP 7.4

  19. 19

    Is there an equal for the c# null coalescing operator in java?

  20. 20

    Is there a way to implement and make use of a "NOT null coalescing" operator?

  21. 21

    Is there an equivalent of isset or a null coalescing operator in SASS?

  22. 22

    Is there a way to implement and make use of a "NOT null coalescing" operator?

  23. 23

    Is there an equivalent of isset or a null coalescing operator in SASS?

  24. 24

    How to define a null-coalescing operator for Twig?

  25. 25

    an expression tree lambda may not contain a null propagating operator

  26. 26

    Lambda Expression using OR operator

  27. 27

    Null-coalescing operator returning null for properties of dynamic objects

  28. 28

    Null propagation + null coalescing operator not working as expected when dealing with InnerException

  29. 29

    Null propagation + null coalescing operator not working as expected when dealing with InnerException

HotTag

Archive