我正在使用Kendo UI MVC包裝創建網格。問題與LINQ查詢
在控制器中的代碼如下:
public ActionResult GetFaxHistory([DataSourceRequest] DataSourceRequest request)
{
using (var faxHistory = new waldenEntities())
{
IQueryable<FaxesSendServer> faxHistoryJson
= (System.Linq.IQueryable<WaldenCompleteFaxWeb.Models.FaxesSendServer>)faxHistory.FaxesSendServers.Where(p => p.UserID.Contains("walden"));
return Json(result, JsonRequestBehavior.AllowGet);
}
}
此代碼的工作,並創建一個沒有問題的網格。當我嘗試選擇特定字段我得到一個錯誤:
我使用的代碼如下:
public ActionResult GetFaxHistory([DataSourceRequest] DataSourceRequest request)
{
using (var faxHistory = new waldenEntities())
{
IQueryable<FaxesSendServer> faxHistoryJson
= (System.Linq.IQueryable<WaldenCompleteFaxWeb.Models.FaxesSendServer>)faxHistory.FaxesSendServers.Where(p => p.UserID.Contains("walden"));
IQueryable faxHistoryJson
= (System.Linq.IQueryable<WaldenCompleteFaxWeb.Models.FaxesSendServer>)faxHistory.FaxesSendServers.Select(c => c.Status);
}
}
我得到的錯誤是低於
System.InvalidCastException was unhandled by user code
Message=Unable to cast object of type 'System.Data.Objects.ObjectQuery1[System.String]' to type 'System.Linq.IQueryable
1[WaldenCompleteFaxWeb.Models.FaxesSendServer]'. Source=WaldenCompleteFaxWeb StackTrace: at WaldenCompleteFaxWeb.Controllers.HomeController.GetFaxHistory(DataSourceRequest request) in C:\waldenltd\Customer Applications\WaldenCompleteFaxWeb\WaldenCompleteFaxWeb\Controllers\HomeController.cs:line 48 at lambda_method(Closure , ControllerBase , Object[]) at System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary2 parameters) at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary
2 parameters) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c_DisplayClass42.b_41() at System.Web.Mvc.Async.AsyncResultWrapper.<>c_DisplayClass81.<BeginSynchronous>b__7(IAsyncResult _) at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult
1.End() at System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c_DisplayClass37.<>c_DisplayClass39.b_33() at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c_DisplayClass4f.b_49() InnerException:
我在做什麼錯?
您的代碼是否可以編譯?兩個查詢結果都使用相同的變量名稱? –
是的。代碼在這兩個例子中編譯。第一個代碼示例(不使用select)不會出錯。選擇示例代碼出錯。爲什麼選擇所有的字段按預期工作,當我做選擇我得到上面的錯誤? – user1181226
那麼至少即時得到一個錯誤說:「一個名爲'faxHistoryJson'的本地變量已經在這個範圍內聲明。」 –