将 IEnumerable 转换为 EntityCollection?

发布于 2024-12-21 14:53:55 字数 2309 浏览 1 评论 0原文

我正在使用 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数据类型,另一个是 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;
    }

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

狼性发作 2024-12-28 14:53:55

首先,EntityCollection实体框架的一部分不是WAF。其次,假设您的类 Exercise 是实体数据模型的一部分,那么您只需将 Exercise 实例添加到新的 EntityCollection 实例并返回它即可:

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")
        };

    var entityCollection = new EntityCollection<Exercise>();

    foreach(var exercise in objs) {
        entityCollection.Add(exercise);
    }

    return entityCollection;
}

First of all, EntityCollection is part of Entity Framework not WAF. Secondly, assuming that your class Exercise is part of the Entity Data Model, then you can just add the Excerciseinstances to a new EntityCollection instance and return it:

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")
        };

    var entityCollection = new EntityCollection<Exercise>();

    foreach(var exercise in objs) {
        entityCollection.Add(exercise);
    }

    return entityCollection;
}
唠甜嗑 2024-12-28 14:53:55

我认为您需要在 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 apply
for all casting, if the value will be null , you will get Exception.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文