Question on updating LINQ compared to updating SQL. help is needed

In the code below, I am updating IndexOrder by ID using TryUpdateModel. Below the update, press DB 2 times. After receiving the current record by identifier, than updating one line.

I am using LINQ To EF

 foreach (var i in indexArraay)
            {
                SubMenu existingMenu = _menu.SubSingle(Int32.Parse(i.id.ToString()));
                existingMenu.IndexOrder = string.IsNullOrEmpty(i.index.ToString()) ? 0 : Int32.Parse(i.index.ToString());
                TryUpdateModel(existingMenu);
                _menu.Add();
            }

In SQL, we do not do this, as in the instructions below.

UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value

Am I mistaken somewhere or is there a better way to write an expression for updating LINQ?

I am not talking about performance here.

+3
source share
1 answer

In LINQ-to-SQL, you can use this Attach. In the code below, the value Orderfor the exisitin menu item with identifier 1 will be updated .

int myId = 1;
int newOrder = 10;

using (var dataContext = new DataContext("..."))
{
    var table = dataContext.GetTable(typeof(Type));
    var menuItem = new MenuItem { Id = myId };
    table.Attach(menuItem);
    menuItem.Order = newOrder;
    dataContext.SubmitChanges();
}

[change]

LINQ-to-Entities .

ObjectSet.Attach Method.

0

All Articles