私はC#、MVC、LINQ、Entity Framework、およびその他すべてのことを始めています。私はこの問題に関するstackoverflowの脳の信頼と他のサイトを選び、それを解決する方法は見当たりません。LINQリポジトリとメソッドreturn
ビルドしている新しいアプリケーションのリポジトリパターンを実装しようとしていますが、クエリ結果を返す際に問題があります。最初の最も重要な問題は、以下のエラーが発生していることです。もう1つは、クエリーから空の結果を処理する方法を理解することです。
この問題では、データベースからダッシュボードビューで表示するリクエストのリストを取得しようとしています。私たちは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>
はViewModelにある:
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; }
}
}
そして、ここでは、私は(残りは実装されていないとの問題を抱えているGetRequestにメソッドへのリポジトリアップしていますまだ):
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();
}
私は取得していますエラーは次のとおりです。
辞書に渡さモデルアイテムタイプ である「System.Collections.Generic.List`1 [RAM.DAL.Models.RequestViewModel]」、 この辞書には「RAMをタイプ のモデル項目が必要.DAL.Models.RequestViewModel '。
実際、ifは@if(!(Model.Any()))である必要があります。そして、明確な答えに感謝します。 – WClark