首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将IEnumerable转换为EntityCollection?

将IEnumerable转换为EntityCollection?
EN

Stack Overflow用户
提问于 2011-12-16 08:46:28
回答 2查看 371关注 0票数 0

我正在使用带有WAF框架的MVVM。WAF框架包含一个名为EntityCollection<T>的类

代码语言:javascript
复制
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),但我有问题,因为数据类型。

编辑:情况是,我想从ExamProduced中插入或添加将函数GetExercises返回到练习属性中的练习。

我的问题是演员阵容。Exercise属性是EntityCollection<Exercise>数据类型,另一个是IEnumerable。如何将IEnumerable中的所有项目插入到EntityCollection中。

代码语言:javascript
复制
    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;
    }
EN

回答 2

Stack Overflow用户

发布于 2011-12-16 09:27:28

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

代码语言:javascript
复制
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;
}
票数 1
EN

Stack Overflow用户

发布于 2011-12-16 07:38:58

我认为您需要在TEntity和XELement之间进行映射。您可以使用AutoMapper框架来做到这一点。

http://automapper.codeplex.com/

我知道这与问题无关,但是如果你想强制转换成int,不要使用(int)objective.Attribute("ID"),使用TryParse,同样适用于所有的强制转换,如果值为null,你会得到异常。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8528568

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档