简体   繁体   中英

ASP.NET MVC System.NotSupportedException LINQ to Entities query

I have written a query which is as follows:

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

Before making some changes this query worked fine and then I made some changes (which I don't remember), then it started issuing this error Image

My Entities are as follows

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; }    
}

You can try this...

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

            });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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