【发布时间】:2011-12-16 00:46:27
【问题描述】:
我正在使用带有 WAF 框架的 MVVM。 WAF 框架包含一个名为EntityCollection<T> 的类:
public sealed class EntityCollection<TEntity> : RelatedEnd, ICollection<TEntity>, IEnumerable<TEntity>, IEnumerable, IListSource where TEntity : class
{
public EntityCollection();
public int Count { get; }
public bool IsReadOnly { get; }
public void Add(TEntity entity);
public void Attach(IEnumerable<TEntity> entities);
public void Clear();
public bool Contains(TEntity entity);
public void CopyTo(TEntity[] array, int arrayIndex);
public ObjectQuery<TEntity> CreateSourceQuery();
public IEnumerator<TEntity> GetEnumerator();
public override void Load(MergeOption mergeOption);
public void OnCollectionDeserialized(StreamingContext context);
public void OnSerializing(StreamingContext context);
public bool Remove(TEntity entity);
}
但是,我需要使用 LINQ TO XML。检查函数 GetExamProduced,我有一个属性练习,其中是 EntityCollection<Exercise>,我需要从 GetExercises(xml) 中添加所有内容,但我遇到了问题,因为数据类型。
编辑: 情况是,我想将返回函数 GetExercises 的练习插入或添加到 ExamProduced 的练习属性中。
我的问题是演员阵容。练习属性是EntityCollection<Exercise> 数据类型,另一个是IEnumerable。如何将 IEnumerable 中的所有项目插入 EntityCollection。
public ExamProduced GetExamProduced(XElement xml)
{
var examProduced = new ExamProduced
{
ExamProducedID = (int)xml.Attribute("ExamID"),
Date = (DateTime)xml.Attribute("Date"),
Seed = (int)xml.Attribute("Seed"),
Exercises = GetExercises(xml)
};
return examProduced;
}
public EntityCollection<Exercise> GetExercises(XElement xml)
{
var objs =
from objective in xml.Descendants("Objective")
where (bool)objective.Attribute("Produced")
let id = (int)objective.Attribute("ID")
select new Exercise
{
ExerciseID = id,
MakeUp = (bool)objective.Attribute("MakeUp"),
Quantify = (byte)(int)objective.Attribute("Quantify"),
Score = (float)objective.Elements().Last().Attribute("Result")
};
return (EntityCollection<Exercise>)objs;
}
【问题讨论】:
-
究竟有什么问题?演员表异常?
-
您遇到错误了吗?还是逻辑不正确?更详细地说明你的情况。您说您需要从 GetExercises(xml) 中添加所有内容。到底是什么??
标签: c# linq linq-to-xml