0

请看下面的代码:

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 吗?

我想要实现的是:

新闻过滤模型列表

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();

谁有更好的主意,请指教。

4

2 回答 2

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
    ;
于 2012-11-07T18:39:31.987 回答
0

CtArticleDetail显然你的and没有关系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;
}
于 2012-11-07T15:48:09.307 回答