[英]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.