if exists statement in sql to linq
It can't be done in LINQ2SQL with a single statement as the LINQ syntax and extension methods don't support inserts. The following (assuming a datacontext named db
) should do the trick.
if (!db.Users.Any( u => u.UserName == "michael" ))
{
db.Users.InsertOnSubmit( new User { UserName = "michael" } );
db.SubmitChanges();
}
Update value if exist or insert in SQL with C# linq
I found the answer.
Here is the code that uses linq and lambda expressions
List<KodikosProtimisis> newListKodikosProtimisis = new List<KodikosProtimisis>();
newListKodikosProtimisis.Add(new KodikosProtimisis { Kodikos = "100", Perigrafi = "Δασμός τρίτων χωρών έναντι όλων (erga omnes)", TaricTiposMetrou = "103" });
newListKodikosProtimisis.Add(new KodikosProtimisis { Kodikos = "110", Perigrafi = "Αυτόνομη δασμολογική αναστολή έναντι όλων (erga omnes) (προσωρινή αναστολή των αυτόνομων δασμών για ορισμένα εμπορεύματα του αγροτικού, χημικού, αεροναυπηγικού και του ηλεκτρονικού τομέα)", TaricTiposMetrou = "112" });
newListKodikosProtimisis.Add(new KodikosProtimisis { Kodikos = "420", Perigrafi = "Ποσόστωση τελωνειακής ένωσης", TaricTiposMetrou = "112" });
newListKodikosProtimisis.Add(new KodikosProtimisis { Kodikos = "422", Perigrafi = "Ποσόστωση τελωνειακής ένωσης", TaricTiposMetrou = "112" });
.
.
.
more values
foreach (var kodikosProtimisis in newListKodikosProtimisis)
{
using (Ektel db = new Ektel(EktelDataContextManager.ConnectionString))
{
var qry = db.KodikosProtimisis.Where(x => x.Kodikos == kodikosProtimisis.Kodikos).FirstOrDefault();
if (qry != null)
{
// The value exists .Update
qry.Kodikos = kodikosProtimisis.Kodikos;
qry.Perigrafi = kodikosProtimisis.Perigrafi;
qry.TaricTiposMetrou = kodikosProtimisis.TaricTiposMetrou;
}
else
{
//The value doesnt exist.Insert.
db.KodikosProtimisis.InsertOnSubmit(new KodikosProtimisis
{
Kodikos = kodikosProtimisis.Kodikos,
Perigrafi = kodikosProtimisis.Perigrafi,
TaricTiposMetrou = kodikosProtimisis.TaricTiposMetrou
});
}
db.SubmitChanges();
}
}
How to do a SQL Where Exists in LINQ to Entities?
The .Any()
extension method typically maps to exists
.
SQL Query to LINQ syntax using not exist and join
You can try like this:
(from lk in localizationKey
where (from l in lang
join lv in localizationValue on l.Id equals lv.LanguageId
where (l.Title == "en-US" && lv.LocalizationKeyId == lk.Id)
select l).FirstOrDefault() == null
select lk).ToList();
or
(from lk in localizationKey
where !(from l in lang
join lv in localizationValue on l.Id equals lv.LanguageId
where !(l.Title == "en-US" && lv.LocalizationKeyId == lk.Id)
select l).FirstOrDefault().Any()
select lk).ToList();
LINQ Query problem. Need to check if exists
Based on the updated question, and if I understand it correctly, I think this solution will work for you.
var urlNameExists = _sb.Any(x => x.UrlName == urlName && x.Id != currentEditId);
if (urlNameExists)
throw Exception("A record with that UrlName already exists");
TryUpdateModel(existingMenu);
_menu.Add();
Related Topics
Sql Query to Sum Fields from Different Tables
Return SQL Rows Where Field Contains Only Non-Alphanumeric Characters
Returning The First X Records in a Postgresql Query with a Unique Field
How to Query Database Name in Oracle SQL Developer
Resources for Database Sharding and Partitioning
How to Get a Real Time Within Postgresql Transaction
How to Emulate Lpad/Rpad with SQLite
Does "Select for Update" Prevent Other Connections Inserting When the Row Is Not Present
How to Check for The SQL Server Version Using Powershell
How to Set List of Values as Parameter into Hibernate Query
Could Not Find Server 'server Name' in Sys.Servers. SQL Server 2014
Sql: Subquery Has Too Many Columns
Pl/Sql- Get Column Names from a Query
How to Use If Statement After a Cte (SQL Server 2005)