作为Multiplesight提供的OdeToFood培训计划的一部分。

错误信息:


类型“ System.NotSupportedException”的异常发生在
EntityFramework.SqlServer.dll,但未在用户代码中处理

附加信息:实体或复杂类型
'OdeToFood.Models.RestaurantListViewModel'无法在
LINQ to Entities查询。


错误来自(_Restaurant.cshtml):

@model IEnumerable<OdeToFood.Models.RestaurantListViewModel>

<div id="restaurantList">
    @foreach (var item in Model)
    {
        <div>
            <h4>@item.Name</h4>
            <div>
            @item.City, @item.Country
            </div>
            <div>
            Reviews: @item.CountOfReviews
            </div>
            <hr />
        </div>

    }
</div>


Index.cshtml

@model IEnumerable<OdeToFood.Models.RestaurantListViewModel>

    @{
        ViewBag.Title = "Home Page";
    }



<form method="get" action="@Url.Action("Index")"
    data-otf-ajax="true" data-otf-target="#restaurantList">

    <input type="search" name="searchTerm" />
    <input type="submit" value="Search By Name" />

</form>
@Html.Partial("_Restaurants", Model)


HomeController.cs

using OdeToFood.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Data.Entity;
using System.Web;
using System.Web.Mvc;

namespace OdeToFood.Controllers
{
    public class HomeController : Controller
    {
        OdeToFoodDb _db = new OdeToFoodDb();

        public ActionResult Index(string searchTerm = null)
        {


            var model =
                _db.Restaurants
                .OrderByDescending(r => r.Reviews.Average(review => review.Rating))
                .Where(r => searchTerm == null || r.Name.StartsWith(searchTerm))
                .Take(10)
                .Select(r => new RestaurantListViewModel
                {
                    Id = r.Id,
                    Name = r.Name,
                    City = r.City,
                    Country = r.Country,
                    CountOfReviews = r.Reviews.Count()
                });

            if (Request.IsAjaxRequest())
            {
                return PartialView("_Restaurants", model);
            }

            return View(model);
        }

        public ActionResult About()
        {
            var model = new AboutModel();
            model.Name = "Tom";
            model.Location = "NYC, USA";

            return View(model);
        }

        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }

        protected override void Dispose(bool disposing)
        {
            if (_db != null)
            {
                _db.Dispose();
            }
            base.Dispose(disposing);
        }
    }
}


我在这里缺少什么吗?对于初学者来说,这个问题没有得到很好的记录,所以我提出了一个问题。如果这是一个愚蠢的错误,我深表歉意。

最佳答案

这是因为数据库不了解您的RestaurantListViewModel,因此EntityFramework无法为此构造底层SQL查询。

因此,额外调用.ToList()应该可以解决问题。

        var model =
            _db.Restaurants
            .OrderByDescending(r => r.Reviews.Average(review => review.Rating))
            .Where(r => searchTerm == null || r.Name.StartsWith(searchTerm))
            .Take(10)

            // The key is here
            .ToList()

            .Select(r => new RestaurantListViewModel
            {
                Id = r.Id,
                Name = r.Name,
                City = r.City,
                Country = r.Country,
                CountOfReviews = r.Reviews.Count()
            });

10-07 23:27