English 中文(简体)
利用林克更新一份名单<T>向行标
原标题:Using Linq to update a List<T> of objects to DB

我有一份表格,向我递交了一张板块物体清单。

我认为,我ed开了几辆平板车,把他们回到我的名单上。

在我的行文中,我有Sessies,我用Automapper 绘制地图,用于平板和后遗体。

现在,我无法向非洲开发银行通报最新情况。

守则:

    [HttpPost]
    public ActionResult Sessies(int id, int? modID, int? projID, string schooljaarparam, List<FlatSessie> sl) {
        if (ModelState.IsValid) {
            foreach (FlatSessie s in sl) { //i run over all FlatSessies which i get
                Models.Sessies ses = Mapper.Map<FlatSessie, Sessies>(s); // i map them to the Sessies object
                List<Sessies> origReeks = _db.Sessies.Where(p => p.Ses_ID == ses.Ses_ID).ToList(); //i get the original Session by ID. if there is a Session with that ID, if not (the ID will be 0) i do an Insert. if there is i want to do an Update.

                if (origReeks.Count > 0) {
                    //it s an update
                    UpdateModel(origReeks.First(); //doesnt work
                    //_db.Sessies.Attach(ses, origReeks.First()); //doesnt work, gives me an error on used ID...
                    _db.SubmitChanges();
                } else {
                    //no sessies yet, add them, this works.
                    _db.Sessies.InsertOnSubmit(ses);
                    _db.SubmitChanges();
                }
            }
            TempData["okmsg"] = "De sessies zijn opgeslagen";
            return RedirectToAction("Index");
        }

        //if not valid, i return the viewdata which i need.

        Module m = _db.Modules.First(md => md.Mod_ID == modID.Value);
        int antses = m.Mod_AantalSessies.Value;

        List<List<SelectListItem>> lpllst = new List<List<SelectListItem>>(antses);


        for (int i = 0; i < antses; i++) {
            lpllst.Add(MvcApplication.lesplaatsList(schooljaarparam, -1));
        }

        ViewData["lesplist"] = lpllst;
        ViewData["lglist"] = MvcApplication.lesgeverList();

        return View(sl);
    }
最佳回答

它可以努力提供prefix至Model(FlatSessie[n],其中n与所涉模型元素的实际投入名称相匹配),以便了解哪些特性在物体上绘制地图,但因为你拿到一份清单可能不会。 您是否尝试利用配对代码中的数据更新检索模型?

而且,一旦你完成这项工作,你可能希望对所有插入/更新(在休息室外)的物品进行单一分类,以便你在一次交易中完成全部提交。 如果存在错误来纠正和重新提交,就会更容易,因为你已经做了一些改动,造成进一步的潜在错误。

问题回答

暂无回答




相关问题
LINQ to SQL as databinding source for WPF Treeview

I wonder if someone could provide a simple example of the following. (preferably in VB.Net): I have an SQL database with related tables and I am successfully using LINQ to SQL in other areas of my ...

Linq to SQL insert/select foreign/primary records

I have table A and Table B. Table B contains two columns, Name and ID. Table A contains several columns and a foreign key column pointing to B.ID called B_ID On the client side, I have all the data I ...

LINQ to SQL Dynamic Sort question

How do I code the Select clause in my LINQ satament to select column aliases so I can sort on them basically I want to accomplish this SQL statement in LINQ: select type_id as id, ...

Using a schema other than dbo in LinqToSql Designer

Is there a way to specify in a data connection or the LinqToSql Designer a schema? Whenever I go to setup a data connection for LinqToSql there doesn t seem to be anyway to specify a schema and I ...

热门标签