44

Could somebody please tell me how I check to see if a record exists, if it does exists then do nothing and if it doesn't then add the record to the database?

Please see my code below:

if (isIpnValidated == true)
{
    using (WebApplication1Entities db = new WebApplication1Entities())
    {
        Orders order = new Orders();
        order.UserId = userId;
        order.Date = System.DateTime.Now;
        order.Transaction = txnId;
        order.Amount = Convert.ToDecimal(mcGross);
        order.Email = payerEmail;
        order.Country = residenceCountry;

        db.Orderss.Add(order);
        db.SaveChanges();
    }
}

I just want to ensure no possible duplication in the database.

10
  • 4
    What constitutes a duplicate? Commented May 14, 2014 at 11:59
  • 1
    Apologies, I want to ensure the txnId does not already exist. Commented May 14, 2014 at 12:00
  • 1
    Your question is entirely unrelated to ASP.NET MVC. Your code looks like you were using Entity Framework though. Did you mean EF 5? Commented May 14, 2014 at 12:01
  • 1
    I cannot use Any with this function, I can use Find, Add, AddRange, etc... but not Any unfortunately. Commented May 14, 2014 at 12:05
  • 2
    @iggyweb Then you're missing a reference to System.Linq. Add it to the top of your file, or get Resharper ;) Commented May 14, 2014 at 12:07

2 Answers 2

76

Use Any:

if (isIpnValidated)
{
    using (WebApplication1Entities db = new WebApplication1Entities())
    {
        if (db.Orderss.Any(o => o.Transaction == txnId)) return;

        Orders order = new Orders();
        order.UserId = userId;
        order.Date = System.DateTime.Now;
        order.Transaction = txnId;
        order.Amount = Convert.ToDecimal(mcGross);
        order.Email = payerEmail;
        order.Country = residenceCountry;

        db.Orderss.Add(order);
        db.SaveChanges();
    }
}
Sign up to request clarification or add additional context in comments.

6 Comments

This assumes he doesn't want to update an existing record...
Yes, see his question: "if it does exists then do nothing"
I definitely don't want to update, only add if the txnId does not exist.
Can I please ask one more thing, how do I check to see if a order actually exists for a user and todays date, I've tried if (db.Orderss.Any(o => o.UserId == userId && o.Date == System.DateTime.Now)) return; but it doesn't appear to work.
What about if (db.Orderss.Any(o => o.UserId == userId && o.Date.Date == System.DateTime.Today)) return;?
|
3
using (WebApplication1Entities db = new WebApplication1Entities())
{
   var order = db.Orders.GetAll().Where(x=> x.Transaction == txnId).FirstOrDefault();
   if(order != null) // update
   {
      //.....
      db.SaveChanges();
    }
   else
   {
      // new
   }
}

3 Comments

No, since he is going to work with order, no need to run the statement twice.
Do you realize you just queried all the orders in the table when you only need one? There is no need for the GetAll()
Besides he is getting all columns , There is no need to FirstOrDefault either , just use count()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.