【发布时间】:2009-06-09 12:35:31
【问题描述】:
我有一个名为 Pathway 的实体对象,它与路径表中的数据直接相关。我的数据库还存储了 Pathway 定制的规则。我想做的是在我的代码中创建一个 Pathway 对象,它是 Pathway + PathwayCustomisations 的结果。结果应该永远不会找到返回数据库的方法,它只是代码中使用的临时投影。
public static Pathway ApplyCustomisation(Pathway p, ICollection<PathwayCustomisation> c)
{
Pathway resultant = new Pathway();
if (!p.PathwayModule.IsLoaded) p.PathwayModule.Load();
foreach (PathwayModule m in p.PathwayModule)
{
resultant.PathwayModule.Add(m);
}
foreach (PathwayCustomisation i in c)
{
switch (i.Command)
{
case "ADD":
resultant.PathwayModule.Add(i.PathwayModule);
break;
case "DELETE":
resultant.PathwayModule.Remove(i.PathwayModule);
break;
}
}
return resultant;
}
此方法在第一个障碍中窒息,因为当 PathwayModule 实体只能属于模型/数据库中的一个时,我将它们添加到第二个 Pathway:
CoursePlanner.Test.PathwayTest.ApplyCustomisation:
System.InvalidOperationException : Collection was modified; enumeration operation may not execute.
有没有办法轻松处理实体投影?我是否正确地解决了这个问题?
编辑:
仅使用方法的第一部分时,我仍然会遇到异常:
public static Pathway ApplyCustomisation(Pathway p, ICollection<PathwayCustomisation> c)
{
Pathway resultant = new Pathway();
if (!p.PathwayModule.IsLoaded) p.PathwayModule.Load();
foreach (PathwayModule m in p.PathwayModule)
{
resultant.PathwayModule.Add(m);
}
return resultant;
}
上面的枚举并没有修改被枚举的同一个集合,它只是将项目添加到第二个集合中。这段代码给出了同样的异常。
.NET3.5、C#、VS Express 2008
谢谢,
丹尼尔
【问题讨论】:
标签: c# .net linq entity-framework orm