我得到以下代碼:
//getting skills from DB
var skills = this.db.HumanSkills.Where(r => r.HumanId == model.Id).Include(x => x.Skill).ToList();
for (int i = 0; i < model.Skills.Count; i ) //iterating over the list. Boundary set to i < model.Skills.Count this is number of elements in my viewmodel (model.Skills) using which I am going to update Name property of my DB elements.
{
if (skills[i] != null) //index out of range here. Why?
{
//here I update DB skill names using model skill names
skills[i].Skill.Name = model.Skills[i].Skill.Name;
}
else //here I create new Skill for every element of the DB skill list that doesn't exist
{
var newSkill = new HumanSkill { Skill = model.Skills[i].Skill, Human = currentHuman };
this.db.Add(newSkill);
this.db.SaveChanges(); //yes, I know it's not a good practice to execute it each time, but otherwise rows in my HumanSkills table are not sorted.
}
}
在線的:
if (skills[i] != null) //index out of range here. Why?
我的索引超出范圍,即使我使用 !=null
uj5u.com熱心網友回復:
在我看來,var 技能與model.Skills是不同的物件
uj5u.com熱心網友回復:
“var Skills”和“model.Skills”是兩個不同的變數。有相同的型別,但它是2個不同的物件!
你可以這樣做:
IEnumerable<Skill> skillsList = this.db.HumanSkills.Where(r => r.HumanId == model.Id).Include(x => x.Skill).ToList();
foreach (Skill s in skillsList)
{
// your logic... or debug.print for check how many items are get from the db
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/408616.html
標籤:
上一篇:無法創建物體框架代碼優先遷移
