2011-12-16 2 views
0

MVVMとWAFフレームワークを使用しています。 WAF Frameworkには、EntityCollectionというクラスがあります。誰かがIEnumberableを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関数をチェックし、EntityCollectionがどこにあり、GetExercises(xml)からすべてを追加する必要があるかのプロパティExercisesがありますが、データ型のために問題があります。前もって感謝します。

EDIT: 状況は、GetExercises関数をExamsProducedからExecisesプロパティに戻す演習を挿入または追加したいと考えています。

私の問題はキャストです。 ExercisesプロパティはEntityCollectionデータ型であり、もう1つは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; 
    } 
+0

正確にどのような問題?キャスト例外はありますか? –

+0

エラーが発生しましたか?またはロジックが間違っていますか?あなたの状況をより詳細に説明してください。 GetExercises(xml)からすべてを追加する必要があると言いました。すべては何ですか? –

+0

編集を確認してください。 – Darf

答えて

0

TEntityとXELementの間をマッピングする必要があると思います。これを行うには AutoMapperフレームワークを使用できます。

http://automapper.codeplex.com/

私はそれが問題に関連していないことを知っているが、あなたがしたい場合は、値はnullになります場合は、同じことが、すべての鋳造のための を適用TryParseを使用し、(int)objective.Attribute("ID")を使用していない をint型にキャストしますあなたは例外を得るでしょう。

1

まず、EntityCollectionEntity FrameworkないWAFの一部です。第二に、あなたのクラスExerciseは、エンティティデータモデルの一部であり、その後、あなただけの新しいEntityCollectionインスタンスにExcerciseインスタンスを追加し、それを返すことができると仮定:

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