logo
down
shadow

How exactly does InsertAllOnSubmit works in Linq


How exactly does InsertAllOnSubmit works in Linq

By : user2948149
Date : November 14 2020, 04:48 PM
I think the issue was by ths following , I have no idea why it doesn't compile, according to the discussion under your question you possibly paste some other code, however this should definitely work:
code :
public ActionResult Order()
{           
    List<Product> products=TempData["products"] as List<Product>;

    foreach (var item in products)
    {
        Order order = new Order();
        order.Customer_Id = 1;
        order.Product_Name = item.Model_Name;
        order.Amount = item.Price;
        order.Order_Date = DateTime.Now.ToLocalTime();
        db.Orders.Add(order);
    }                 

    db.SaveChanges();
    return View(products);
}


Share : facebook icon twitter icon
Linq To SQL InsertAllOnSubmit and DeleteAllOnSubmit

Linq To SQL InsertAllOnSubmit and DeleteAllOnSubmit


By : romytiu
Date : March 29 2020, 07:55 AM
this will help Can I revert deleted objects from DeleteAllOnSubmit() by calling InsertAllOnSubmit() again, or the opposite? , You can retrieve the original values by calling:
code :
GetOriginalEntityState()
Linq to SQL, InsertOnSubmit vs. InsertAllOnSubmit performance?

Linq to SQL, InsertOnSubmit vs. InsertAllOnSubmit performance?


By : Ekkachaibeer
Date : March 29 2020, 07:55 AM
I wish this help you There is no difference at all, InsertAllOnSubmit actually calls the InsertOnSubmit, here is the code for InsertAllSubmit:
code :
public void InsertAllOnSubmit<TSubEntity>(IEnumerable<TSubEntity> entities) where TSubEntity : TEntity
{
    if (entities == null)
    {
        throw Error.ArgumentNull("entities");
    }
    this.CheckReadOnly();
    this.context.CheckNotInSubmitChanges();
    this.context.VerifyTrackingEnabled();
    List<TSubEntity> list = entities.ToList<TSubEntity>();
    using (List<TSubEntity>.Enumerator enumerator = list.GetEnumerator())
    {
        while (enumerator.MoveNext())
        {
            TEntity entity = (TEntity)((object)enumerator.Current);
            this.InsertOnSubmit(entity);
        }
    }
}
InsertAllOnSubmit (LINQ to SQL) generates duplicate entries in related tables

InsertAllOnSubmit (LINQ to SQL) generates duplicate entries in related tables


By : Jack Zhao
Date : March 29 2020, 07:55 AM
Linq InsertAllOnSubmit if not exist

Linq InsertAllOnSubmit if not exist


By : Shivam Mishra
Date : March 29 2020, 07:55 AM
To fix the issue you can do One query to figure out what needs to be inserted, then after some in-memory manipulation you can InsertAllOnSubmit:
code :
using(MyDataContext db = new MyDataContext())
{
    var itemNames = Items.Select(i=>i.name).ToList();
    var itemNamesInDb = db.Items.Where(i=>itemNames.Contains(i.Name)).Select(i=>i.Name).ToList();
    var itemNamesNotInDb = new HashSet<string>(itemNames.Except(itemNamesInDb));
    var itemsNotInDb = Items.Where(i=>itemNamesNotInDb.Contains(i.name)).ToList();

    db.Items.InsertAllOnSubmit(itemsNotInDb);
    db.SubmitChanges();
}
InsertAllOnSubmit<TSubEntity> when to use?

InsertAllOnSubmit<TSubEntity> when to use?


By : Iññøcènt Råscal
Date : March 29 2020, 07:55 AM
Any of those help It is a convenience method that allows you to insert multiple entities in one call. This saves you the foreach loop. It does not change the way the database is accessed but it might be a little less CPU efficient according to the decompiled code (tiny effect).
The generic type parameter exists so that you can pass a derived type if your entities are in an inheritance relationship. Starting with C# covariance support this is no longer needed.
shadow
Privacy Policy - Terms - Contact Us © ourworld-yourmove.org