System.Linq.IQueryable错误

时间:2013-03-08 19:36:55

标签: c# asp.net-mvc linq iqueryable

我正在开发一个ASP.NET MVC(旧版本)应用程序,我需要创建一个表单来根据两个参数查找预订:Date和RoomID。数据库中有基础表:由RoomID链接的ReservationRequests和Rooms。

我在存储库中写了这个查询:

public IQueryable<ReservationRequest> FindReservationRequestInfo(DateTime date, int roomID)
    {
        return from requests in FindAllReservationRequests()
                where requests.Date == date

                    & requests.RoomID == roomID

                select requests;
    }

模型看起来像:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
namespace RoomReservations.Models
{
    public class FindReservationRequestInfo
{
    public IQueryable<string> ReservationRequestsInfo { get; set; }
    public DateTime Date { get; set; }

    public SelectList Rooms { get; set; }

    public FindReservationRequestInfo(IQueryable<string> reservationRequestsInfo, DateTime date, SelectList rooms)
    {
        ReservationRequestsInfo = reservationRequestsInfo;
        Date = date;
        Rooms = rooms;
    }
}

}

Controller中的代码是:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using RoomReservations.Models;
using RoomReservations.Helpers;
using Telerik.Web.Mvc;


namespace RoomReservations.Controllers
{
    public class ReportsController : Controller
{
    IRepository Repository;

    public ReportsController()
        : this(new Repository())
    {
    }

    public ReportsController(IRepository repository)
    {
        Repository = repository;
    }


    //
    // GET: /Reports/FindReservationRequestInfo
    [Authorize(Roles = "Administrator")]
    public ActionResult FindReservationRequestInfo()
    {
        IEnumerable<Room> rooms = Repository.FindAllRooms();
        rooms = rooms.Concat(new List<Room>() { new Room() }.AsEnumerable()).OrderBy(o => o.ID);

        return View(new FindReservationRequestInfo(
            new List<string>().AsQueryable(), DateTime.Today,
            new SelectList(rooms, "ID", "Name")));
    }

    //
    // POST: /Reports/FindReservationRequestInfo
    [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrator"), ValidateAntiForgeryToken]
    public ActionResult FindReservationRequestInfo(FormCollection collection)
    {
        IQueryable<string> reservationRequestsInfo;
        DateTime date = DateTime.Today;

        Room room = Repository.GetRoom(Convert.ToInt32(collection["ID"]));

        if (collection["Date.Date"] == String.Empty)
            ModelState.AddModelError("Date.Date", "Date is required");



        if (ModelState.Sum(s => s.Value.Errors.Count()) == 0)
        {
            date = DateTime.Parse(collection["Date.Date"]);

            if (room == null)
            {
                room = new Room();
                reservationRequestsInfo = Repository.FindReservationRequestInfo(date, room.ID);
            }

        }
        else
        {
            reservationRequestsInfo = new List<string>().AsQueryable();
        }

        IEnumerable<Room> rooms = Repository.FindAllRooms();
        rooms = rooms.Concat(new List<Room>() { new Room() }.AsEnumerable()).OrderBy(o => o.ID);

        return View(new FindReservationRequestInfo(
            reservationRequestsInfo, date,
            new SelectList(rooms, "ID", "Name", room.ID)));
    }
 }

}         //

我不确定我在Controller中的代码有什么问题。如果有人能帮助我理解我做错了什么,我将非常感激。 非常感谢你提前!

1 个答案:

答案 0 :(得分:2)

您的查询正在返回

IQueryable<ReservationRequest>

但在您的控制器中,您将其设置为等于您声明为

的变量
IQueryable<string>

所以这一点

    [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrator"), ValidateAntiForgeryToken]
    public ActionResult FindReservationRequestInfo(FormCollection collection)
    {
        IQueryable<string> reservationRequestsInfo;
        //...

应该是

    [AcceptVerbs(HttpVerbs.Post), Authorize(Roles = "Administrator"), ValidateAntiForgeryToken]
    public ActionResult FindReservationRequestInfo(FormCollection collection)
    {
        IQueryable<ReservationRequest> reservationRequestsInfo;
        //...

然后适当地重构以适应变化的级联效应。