How to use Linq as IF - condition

I received a small linq request to find a client in a database:

var query =
   from c in database.customer
   where c.ID == input
   select c;

Each client has an identifier, which in this case is set by the user - "enter"

All clients in the database are owned by Germany, but some of them must be copied to the database with a different value for the country (England instead of Germany).

Now, before adding them directly to the database, I want to check if the client has an “English version” in the database.

if (query.Where(c => c.country == "England"))
   //do nothing
else 
   //add the customer with c.country = "England"

The problem is that this is not a regular if statement.

Is there a way to achieve what I want to express in this if-state?

thank

+4
source share
1

if (query.Any(c => c.country.Equals("England")))
   //do nothing
else 
   //add the customer with c.country = "England"

Linq Where IEnumerable, bool. , Any All, true false , true .

+8

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


All Articles