繁体   English   中英

ASP.NET MVC System.NotSupportedException LINQ到实体查询

[英]ASP.NET MVC System.NotSupportedException LINQ to Entities query

我写了一个查询,如下所示:

var model = (from r in _db.Restaurants
     join rev in _db.Reviews
     on r.Id equals rev.RestaurantId
     into rest_rev
     from rr in rest_rev.DefaultIfEmpty()
     select new RestaurantViewModel
     {
        Id=r.Id,
        Name=r.Name,
        City=r.City,
        Country=r.Country,
        NumberofReviews=r.Reviews.Count,
        Reviews=rr.Body
     });

在进行一些更改之前,此查询工作正常,然后进行了一些更改(我不记得了),然后开始发出此错误图像。

我的实体如下

public class Restaurant
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public virtual ICollection<RestaurantReview> Reviews { get; set; }
}

public class RestaurantReview
{
    public int Id { get; set; }
    public int Rating { get; set; }
    public string ReviewerName { get; set; }
    public string Body { get; set; }

    public int RestaurantId { get; set; }
}

public class RestaurantViewModel
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string City { get; set; }
    public string Country { get; set; }
    public int NumberofReviews { get; set; }
    public string Reviews { get; set; }    
}

你可以试试这个...

List<RestaurantViewModel> listViewModel = new List<RestaurantViewModel>();

var model =  _db.Restaurants.Include("Reviews").ToList().ForEach((item) =>
            {
                RestaurantViewModel viewmodel = new RestaurantViewModel();

               viewmodel.ID = item.ID
               viewmodel.NumberofReviews = item.Reviews.Count;
               ....

                listViewModel.Add(viewmodel);

            });

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM