请参阅以下代码:
public class NewsFilterModel
{
public CtArticleDetail ArticleDetail { get; set; }
public PcPage Page { get; set; }
}
public IEnumerable<NewsFilterModel> GetNewsFilterModelByTagNames(string tagNames, int status)
{
var resultArticle = db.CtArticleDetail.Where(m => m.tagNames == tagNames);
var resultPage = db.PcPage.Where(m => m.tagNames == tagNames);
}
任何人都可以建议将两个模型组合成一个模型的最佳方法,即NewsFilterModel?
我想要实现的目标是:
NewsFilterModelList
1.Article1
2.Page1
3.Article2
4.Page2
5.Article3
6.Page3
我的结尾如下:
foreach (var item in pageResult)
{
resultList.Add(new NewsFilterModel { ArticleDetail = null, Page = item, Date = item.UpdatedOn.ToDateTime() });
}
foreach (var item in tempArticleList)
{
resultList.Add(new NewsFilterModel { ArticleDetail = item, Page = null, Date = item.LiveDate.ToDateTime() });
}
return resultList.AsEnumerable();
任何人都有更好的想法,请指教。
答案 0 :(得分:1)
您可以使用Union来组合结果集
var query1 = db.CtArticleDetail
.Where(a => a.tagNames.Equals(tagNames))
.Select(a => new NewsFilterModel() { ArticleDetail = a, Page = null });
var query2 = db.PcPage
.Where(a => a.tagNames.Equals(tagNames))
.Select(a => new NewsFilterModel() { ArticleDetail = null, Page = a });
var query3 = query1.Union(query2)
//.OrderBy(a => a.Date); -- Order here
;
答案 1 :(得分:0)
显然,您的CtArticleDetail
和PcPage
没有任何关系,因此您需要更改模型。
public class NewsFilterModel
{
public IEnmerable<CtArticleDetail> ArticleDetails { get; set; }
public IEnmerable<PcPage> Pages { get; set; }
}
和
public NewsFilterModel GetNewsFilterModelByTagNames(string tagNames, int status)
{
var resultArticle = db.CtArticleDetail.Where(m => m.tagNames == tagNames);
var resultPage = db.PcPage.Where(m => m.tagNames == tagNames);
var newsFilterModel = new NewFilterModel { ArticleDetails = resultArticle, Pages = resultPage };
return newsFilterModel;
}