【问题标题】:Casting a IEnumerable to EntityCollection?将 IEnumerable 转换为 EntityCollection?
【发布时间】: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&lt;Exercise&gt;,我需要从 GetExercises(xml) 中添加所有内容,但我遇到了问题,因为数据类型。

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

我的问题是演员阵容。练习属性是EntityCollection&lt;Exercise&gt; 数据类型,另一个是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


【解决方案1】:

首先,EntityCollectionEntity Framework 的一部分,而不是 WAF。其次,假设您的类Exercise 是实体数据模型的一部分,那么您只需将Excerciseinstances 添加到新的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;
}

【讨论】:

    【解决方案2】:

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

    http://automapper.codeplex.com/

    我知道这与问题无关,但如果你想转换为 int 不要使用(int)objective.Attribute("ID"),使用TryParse,同样适用 对于所有转换,如果值为 null ,您将获得异常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多