将 IEnumerable 转换为 EntityCollection?
我正在使用 MVVM 和 WAF 框架。 WAF 框架包含一个名为 EntityCollection
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,我有一个属性Exercises,其中是EntityCollection
,我需要从GetExercises(xml) 添加所有内容,但由于数据类型,我遇到了问题。
编辑: 情况是,我想插入或添加将函数 GetExercises 返回到 ExamProduced 的练习属性中的练习。
我的问题是演员阵容。 Exams 属性是 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;
}
I'm using MVVM with WAF framework. WAF Framework contains a class called 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);
}
But, I need to use LINQ TO XML. Check the function GetExamProduced, I've got a property Exercises, where is a EntityCollection<Exercise>
and I need to add all from GetExercises(xml), but I'm having problems because the datatypes.
EDIT:
The situation is, I'd like to insert or add the exercises which returns the function GetExercises into Exercises property from ExamProduced.
My problem is the cast. Exercises property is EntityCollection<Exercise>
datatype, and the another is an IEnumerable. How can I do to insert all the items from IEnumerable into the 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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
首先,
EntityCollection
是实体框架的一部分不是WAF。其次,假设您的类Exercise
是实体数据模型的一部分,那么您只需将Exercise
实例添加到新的 EntityCollection 实例并返回它即可:First of all,
EntityCollection
is part of Entity Framework not WAF. Secondly, assuming that your classExercise
is part of the Entity Data Model, then you can just add theExcercise
instances to a new EntityCollection instance and return it:我认为您需要在 TEntity 和 XELement 之间进行映射。您可以使用
AutoMapper框架就是这样做的。
http://automapper.codeplex.com/
我知道这与问题无关,但如果你愿意转换为 int
不要使用
(int)objective.Attribute("ID")
,使用 TryParse,同样适用对于所有转换,如果值为 null ,您将得到异常。
I tink that you need to map between TEntity and XELement. You can use
AutoMapper framework to do so.
http://automapper.codeplex.com/
I know that it's not related to the question, but if you want to cast to int
do not use
(int)objective.Attribute("ID")
,use TryParse, the same applyfor all casting, if the value will be null , you will get Exception.