2016-11-03 11 views
0

私にはニュースがあります。 1つのニュースは多くのタグ、カテゴリ、画像を持つことができます。私は拡張モデルについてそれを特別なものにしています。デフォルトと1対多数「

return 
    ((from n in db.News 
     from i in db.Images.DefaultIfEmpty() 
     from t in db.Tags.DefaultIfEmpty() 
     from c in db.Categories.DefaultIfEmpty() 
     select new NewsExt() 
     { 
      News = n, 
      Images = i, 
      Tags = t, 
      Categories = c 
     }).GroupBy(news => news.News)) 

私はNewsExtの*配列をしたい** 1つのニュースや他のすべてのエンティティを繰り返すことなく:私は第二の変形と協力し、このような何かをしたい

public class NewsExt 
    { 
     public News News { get; set; } 
     public List<Categories> Categories { get; set; } 
     public List<Images> Images { get; set; } 
     public List<Tags> Tags { get; set; } 
    } 

または

public class NewsExt 
    { 
     public Base.News News { get; set; } 
     public Categories Categories { get; set; } 
     public Images Images { get; set; } 
     public Tags Tags { get; set; } 
    } 

この配列の各要素に最初のviewmodelについては 私が試してみてください。

(from n in db.News 
      select new NewsExt() 
      { 
       News = n, 
       Images = (from i in db.Images.DefaultIfEmpty() 
         from n_i in db.News_Image 
         where n_i.news_id == n.id 
         select i).ToList(), 

       Tags = (from t in db.Tags.DefaultIfEmpty() 
         from n_t in db.News_Tag 
         where n_t.news_id == n.id 
         select t).ToList(), 

       Categories = (from c in db.Categories.DefaultIfEmpty() 
          from n_c in db.News_Category 
          where n_c.news_id == n.id 
          select c).ToList() 
      }).ToList(); 

しかし、過剰レコードを持っています。

+0

あなたが望むものは一切明確ではありません - あなたは_単一のカテゴリまたはカテゴリのリストを望みますか?それはどちらかを持つことができる構造をしようとしているようですが、これは不可能です(1つのエントリでカテゴリのリストを持つことはできますが、1つのカテゴリしか持たないのと同じです) –

+0

リストが必要ですこのニュースを持っているカテゴリのニュースには1つのカテゴリはありません。 – ExUser

答えて

0

私はの最初のビューモデルを使用することにしました。

(from n in db.News 
select new NewsExt() 
{ 
    News = n, 
    Images = (from i in db.Images.DefaultIfEmpty() 
       join n_i in db.News_Image on i.id equals n_i.image_id 
       where n_i.news_id == n.id 
       select i).ToList(), 

    Tags = (from t in db.Tags.DefaultIfEmpty() 
      join n_t in db.News_Tag on t.id equals n_t.tag_id 
      where n_t.news_id == n.id 
      select t).ToList(), 

     Categories = (from c in db.Categories.DefaultIfEmpty() 
        join n_c in db.News_Category on c.id equals n_c.category_id 
        where n_c.news_id == n.id 
        select c).ToList() 
       }).ToList();