在Linq中设置其他函数的属性列表数据?

时间:2018-02-05 04:14:24

标签: c# linq

我上课 新闻 。每条新闻都有 相关新闻 来自

  

功能 GetRelated (新闻新闻)返回列表<新闻和GT;

我创建 NewsTagsCommodityViewModel 来存储新闻及相关新闻

public class NewsTagsCommodityViewModel
{
    public News News { get; set; }
    public List<News> RelatedNews { get; set; }
}
public class NewsDao
{
    //....
    List<News> GetRelated(News news)
    {
          //....
          return res;
    }

    public IEnumerable<NewsTagsCommodityViewModel> ListAllNews()
    {
        var model = db.News.Where(s => s.Status)
                        .Select(s => new NewsTagsCommodityViewModel
                        {
                            News = s,
                            RelatedNews = GetRelated(s)
                        });
        return model.OrderByDescending(x => x.News.CreatedDate);
    }
}

当调用ListAllNews()时,它抛出异常:

  

LINQ to Entities无法识别方法&#39; System.Collections.Generic.List`1 [Data.Entity.News] GetRelated(Data.Entity.News)&#39;方法,并且此方法无法转换为商店表达式。

如何解决这个问题?

新闻代码

public partial class News
    {
        public long ID { get; set; }

        [Required]
        [StringLength(500)]
        public string Title { get; set; }

        [StringLength(500)]
        public string MetaTitle { get; set; }

        public string Description { get; set; }

        [Column(TypeName = "ntext")]
        public string ContentHtml { get; set; }

        [StringLength(250)]
        public string Images { get; set; }

        [StringLength(250)]
        public string MetaKeywords { get; set; }

        public string MetaDescription { get; set; }

        public bool Status { get; set; }

        public DateTime CreatedDate { get; set; }

        [StringLength(50)]
        public string CreatedBy { get; set; }

        public DateTime? UpdatedDate { get; set; }

        [StringLength(50)]
        public string UpdatedBy { get; set; }

        [StringLength(500)]
        public string RelatedNews { get; set; }

        public long? CategoryID { get; set; }

        public int? CommodityID { get; set; }

        public int? ViewCount { get; set; }

        [StringLength(500)]
        public string Tags { get; set; }

        public bool UpTopNew { get; set; }

        public bool UpTopHot { get; set; }

        [StringLength(1000)]
        public string Name { get; set; }

        [StringLength(2)]
        public string LangCode { get; set; }

        public long? RefID { get; set; }
    }

实施 GetRelated()方法。

private List<News> GetRelated(News news)
        {
            var listChild = new RouteDao().GetChildrents((int)news.CommodityID);
            var data = db.News.Where(s => s.Status && s.LangCode == news.LangCode).ToList()
                              .Where(s => (s.CommodityID == (int)news.CommodityID) || listChild.Any(x => x.ID == s.CommodityID) || s.CategoryID == news.CategoryID);
            return ListEntityHelpper.PickRandom(data.Where(x => x.ID != news.ID).ToArray(), 3);
        }

2 个答案:

答案 0 :(得分:1)

SQL Server或您正在使用的任何数据存储不知道<html lang="pl"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <!-- Bootstrap CSS --> <link rel="stylesheet" href="./css/bootstrap.min.css"> <title>Strona w przebudowie</title> </head> <body> <div class="row h-100"> <div class="col-sm-12 my-auto"> <h1 class="display-4">STRONA W PRZEBUDOWIE</h1> </div> </div> <script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script> <script src="./js/bootstrap.min.js"></script> </body> 是什么,因此它无法将其映射到SQL查询。您可以组合您的查询(这是更可取的),或者在您获取新闻后将相关新闻添加到模型中。

答案 1 :(得分:0)

我收到此异常,因为在NewsDao()类中创建的函数 GetRelated()不在SQL Server中。

我通过更改修复它:

 public IEnumerable<NewsTagsCommodityViewModel> ListAllNews()
 {
        var model = db.News.Where(s => s.Status)
                        .Select(s => new NewsTagsCommodityViewModel
                        {
                            News = s,
                            RelatedNews = GetRelated(s)
                        });
        return model.OrderByDescending(x => x.News.CreatedDate);
 }

要:

public IEnumerable<NewsTagsCommodityViewModel> ListAllNew()
{
    var model = db.News.Where(s => s.Status)
                    .Select(s => new NewsTagsCommodityViewModel
                    {
                        News = s
                    }).ToList();
    model.ForEach(s => s.RelatedNews = GetRelated(s.News));
    return model.OrderByDescending(x => x.News.CreatedDate);
}

==&GT;但这种方式循环2次model =&gt;表现不佳。

也许有更好的方法来解决它!