我有新闻。一个新闻可以有许多标签,类别和图像。关于扩展模型我很特别。
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; }
}
我使用第二种变体并想要这样的东西:
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数组**包含一个新闻和所有其他实体,而不重复此数组的每个元素。 对于第一个视图模型,我尝试:
(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 :(得分: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();