Skip to content
Advertisement

Update value if exist or insert in SQL with C# linq

I have this code which is working fine and I update a value if exists or insert if not

With this code I manage to do the work with only one query to the SQL, but I was wondering if there is an easier or more elegant way to do it.

For example if I could use linq and lambda expressions (which I don’t have much experience with).

I want (if it’s possible) to remove the query and put something that I can control more easy and most important to maintain it easily if something changes, for example database design or massively change something in the existing values.

EDIT: I really don’t understand why I have the negative votes. I give a specific code that already works and I ask if there is a better way.

Advertisement

Answer

I found the answer. Here is the code that uses linq and lambda expressions

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement