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

List<KodikosProtimisis> newList = new List<KodikosProtimisis>();
newList.Add(new KodikosProtimisis { Kodikos = "100", Perigrafi = "Description", TaricTiposMetrou = "103" });
newList.Add(new KodikosProtimisis { Kodikos = "110", Perigrafi = "Description", TaricTiposMetrou = "112" });
newList.Add(new KodikosProtimisis { Kodikos = "420", Perigrafi = "Description", TaricTiposMetrou = "112" });
.
.
.
(more values)

string connectionString = Configuration.Parameters.Config.ConnectionString;

string query = @"IF EXISTS(SELECT * FROM dbo.KodikosProtimisis WHERE Kodikos = @kodikos and DiasafistisId=@diasafistisId)
                        UPDATE dbo.KodikosProtimisis 
                        SET Kodikos = @kodikos, DiasafistisId=@diasafistisId,Perigrafi=@perigrafi,TaricTiposMetrou = @taricTiposMetrou
                        WHERE Kodikos = @kodikos and DiasafistisId=@diasafistisId or DiasafistisId is null
                    ELSE
                        INSERT INTO dbo.KodikosProtimisis(Kodikos, Perigrafi,TaricTiposMetrou, DiasafistisId) VALUES(@kodikos, @perigrafi,@taricTiposMetrou, @diasafistisId);";

foreach (var person in newList)
{
    using (SqlConnection conn = new SqlConnection(connectionString))
    using (SqlCommand cmd = new SqlCommand(query, conn))
    {
        //δίνουμε τις παραμέτρους στο ερώτημα μας 
        cmd.Parameters.Add("@kodikos", SqlDbType.NVarChar, 100).Value = person.Kodikos;
        cmd.Parameters.Add("@perigrafi", SqlDbType.NVarChar, 200).Value = person.Perigrafi;
        cmd.Parameters.Add("@diasafistisId", SqlDbType.Int, 100).Value = DefaultDiasafistis.DiasafistisDefault.Id;
        cmd.Parameters.Add("@taricTiposMetrou", SqlDbType.NVarChar, 150).Value = person.TaricTiposMetrou;

        conn.Open();
        int rowsAffected = cmd.ExecuteNonQuery();
        conn.Close();
    }
}

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

        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();
                    }

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