LINQ 存储库和方法 return
LINQ Repository & method return
我是 C#、MVC、LINQ、Entity Framework 以及我正在做的所有其他事情的新手。我已经在这个问题上选择了 Whosebug brain-trust 和其他网站,但看不到如何解决它。
我正在尝试为我们正在构建的新应用程序实现存储库模式,但在返回查询结果时遇到问题。第一个也是最紧迫的问题是我收到以下错误,第二个问题更多的是弄清楚如何处理查询中的空结果。
对于这个问题,我正在尝试从数据库中获取请求列表以显示在仪表板视图中。我们有一个使用 SQL 查询的工作原型,我正在尝试用存储库替换它,这可能有点过分,但这是我们想要做的事情。
视图如下:
@using RAM.DAL.Models
@model RequestViewModel
@{
ViewBag.Title = "Dashboard";
}
<h1>Dashboard</h1>
<div class="col-md-4">
<h2>Resource Requests</h2>
<div class="panel">
<table class="table table-hover">
<thead>
<tr class="bg-primary">
<th>Number</th>
<th>Mission Title</th>
<th>Resource Requested</th>
<th>Person</th>
</tr>
</thead>
<tbody>
@if (Model.isEmpty)
{
<tr>
<td colspan="4">No requests pending</td>
</tr>
}
else
{
<tr onclick="location.href= '@Url.Action("Assignment", "Mission", new { id = Model.ID })'">
<td>@Model.ID</td>
<td>@Model.title</td>
<td>@Model.resourceTitle</td>
<td>@Model.userName</td>
</tr>
}
</tbody>
</table>
</div>
<!--<p><a class="btn btn-default" href="#">Content 1 Btn »</a></p>-->
</div>
这是视图模型:
using System;
namespace RAM.DAL.Models
{
public class RequestViewModel
{
public int? ID { get; set; }
public string title { get; set; }
public string requestText { get; set; }
public string user_ID { get; set; } //The userID of the user being requested.
public string userName { get; set; } //Full name of the user being requested
public int? fromResourceID { get; set; } //The resource where the request was generated from
public int? toResourceID { get; set; } //The resource where the reassigned worker is requested to go to
public string resourceTitle { get; set; } //Title of the resource where the reassigned worker is requested to go to
public DateTime? requestDate { get; set; }//The date the request was made
public bool? isEmpty { get; set; }
}
}
这是我遇到问题的 GetRequests 方法的存储库(其余尚未实现):
using RAM.DAL.Models;
using System;
using System.Linq;
using System.Data;
using System.Collections.Generic;
namespace RAM.DAL
{
public class RequestRepository : IRequestRepository<RequestViewModel>
{
private RAMcontext context;
public RequestRepository(RAMcontext context)
{
this.context = context;
}
public IEnumerable<RequestViewModel> GetRequests()
{
var requests = from r in context.RAM_Requests
join u in context.Users on r.user_ID equals u.User_ID
join res in context.RAM_Resources on r.toResourceID equals res.ID
where r.resolved == false
select new RequestViewModel()
{
title = r.title,
ID = r.ID,
fromResourceID = r.fromResourceID,
toResourceID = r.toResourceID,
user_ID = r.user_ID,
userName = u.First_Name + " " + u.Last_Name,
resourceTitle = res.title,
requestText = r.requestText,
requestDate = r.requestDate
};
/* }
catch
{
RequestViewModel empty = new RequestViewModel
{
isEmpty = true
};
return empty;
}*/
return requests.ToList().AsEnumerable();
}
我得到的错误是:
The model item passed into the dictionary is of type
'System.Collections.Generic.List`1[RAM.DAL.Models.RequestViewModel]',
but this dictionary requires a model item of type
'RAM.DAL.Models.RequestViewModel'.
根据错误消息,我猜您的操作方法正在向视图发送 RequestViewModel
的集合。但是您的视图被强类型化为 RequestViewModel
的单个实例,而不是集合。这就是您收到此错误的原因。
既然要显示请求的集合,就应该将视图更改为强类型集合。您可以使用 LINQ Any()
方法来确定集合中是否有多个项目传递给视图,并 show/hide 一条消息/ table 来显示数据。
@model IEnumerable<RequestViewModel>
<h1>Dashboard</h1>
@if(!Model.Any())
{
<p>No records found </p>
}
else
{
<table>
<tr>
<th>Title</th>
<th>User name </th>
</tr>
@foreach(var req in Model)
{
<tr>
<td>@req.title</td>
<td>@req.userName</td>
</tr>
}
</table>
}
我是 C#、MVC、LINQ、Entity Framework 以及我正在做的所有其他事情的新手。我已经在这个问题上选择了 Whosebug brain-trust 和其他网站,但看不到如何解决它。
我正在尝试为我们正在构建的新应用程序实现存储库模式,但在返回查询结果时遇到问题。第一个也是最紧迫的问题是我收到以下错误,第二个问题更多的是弄清楚如何处理查询中的空结果。
对于这个问题,我正在尝试从数据库中获取请求列表以显示在仪表板视图中。我们有一个使用 SQL 查询的工作原型,我正在尝试用存储库替换它,这可能有点过分,但这是我们想要做的事情。
视图如下:
@using RAM.DAL.Models
@model RequestViewModel
@{
ViewBag.Title = "Dashboard";
}
<h1>Dashboard</h1>
<div class="col-md-4">
<h2>Resource Requests</h2>
<div class="panel">
<table class="table table-hover">
<thead>
<tr class="bg-primary">
<th>Number</th>
<th>Mission Title</th>
<th>Resource Requested</th>
<th>Person</th>
</tr>
</thead>
<tbody>
@if (Model.isEmpty)
{
<tr>
<td colspan="4">No requests pending</td>
</tr>
}
else
{
<tr onclick="location.href= '@Url.Action("Assignment", "Mission", new { id = Model.ID })'">
<td>@Model.ID</td>
<td>@Model.title</td>
<td>@Model.resourceTitle</td>
<td>@Model.userName</td>
</tr>
}
</tbody>
</table>
</div>
<!--<p><a class="btn btn-default" href="#">Content 1 Btn »</a></p>-->
</div>
这是视图模型:
using System;
namespace RAM.DAL.Models
{
public class RequestViewModel
{
public int? ID { get; set; }
public string title { get; set; }
public string requestText { get; set; }
public string user_ID { get; set; } //The userID of the user being requested.
public string userName { get; set; } //Full name of the user being requested
public int? fromResourceID { get; set; } //The resource where the request was generated from
public int? toResourceID { get; set; } //The resource where the reassigned worker is requested to go to
public string resourceTitle { get; set; } //Title of the resource where the reassigned worker is requested to go to
public DateTime? requestDate { get; set; }//The date the request was made
public bool? isEmpty { get; set; }
}
}
这是我遇到问题的 GetRequests 方法的存储库(其余尚未实现):
using RAM.DAL.Models;
using System;
using System.Linq;
using System.Data;
using System.Collections.Generic;
namespace RAM.DAL
{
public class RequestRepository : IRequestRepository<RequestViewModel>
{
private RAMcontext context;
public RequestRepository(RAMcontext context)
{
this.context = context;
}
public IEnumerable<RequestViewModel> GetRequests()
{
var requests = from r in context.RAM_Requests
join u in context.Users on r.user_ID equals u.User_ID
join res in context.RAM_Resources on r.toResourceID equals res.ID
where r.resolved == false
select new RequestViewModel()
{
title = r.title,
ID = r.ID,
fromResourceID = r.fromResourceID,
toResourceID = r.toResourceID,
user_ID = r.user_ID,
userName = u.First_Name + " " + u.Last_Name,
resourceTitle = res.title,
requestText = r.requestText,
requestDate = r.requestDate
};
/* }
catch
{
RequestViewModel empty = new RequestViewModel
{
isEmpty = true
};
return empty;
}*/
return requests.ToList().AsEnumerable();
}
我得到的错误是:
The model item passed into the dictionary is of type 'System.Collections.Generic.List`1[RAM.DAL.Models.RequestViewModel]', but this dictionary requires a model item of type 'RAM.DAL.Models.RequestViewModel'.
根据错误消息,我猜您的操作方法正在向视图发送 RequestViewModel
的集合。但是您的视图被强类型化为 RequestViewModel
的单个实例,而不是集合。这就是您收到此错误的原因。
既然要显示请求的集合,就应该将视图更改为强类型集合。您可以使用 LINQ Any()
方法来确定集合中是否有多个项目传递给视图,并 show/hide 一条消息/ table 来显示数据。
@model IEnumerable<RequestViewModel>
<h1>Dashboard</h1>
@if(!Model.Any())
{
<p>No records found </p>
}
else
{
<table>
<tr>
<th>Title</th>
<th>User name </th>
</tr>
@foreach(var req in Model)
{
<tr>
<td>@req.title</td>
<td>@req.userName</td>
</tr>
}
</table>
}