我有两个相互关系的表。我希望能够在一个视图中同时查看信息。但是,当我尝试使用VolumeRates表中的属性时,出现错误:
错误 CS1061"ICollection"不包含"BidRate"的定义,并且找不到接受类型为"ICollection"的第一个参数的扩展方法"BidRate"(是否缺少 using 指令或程序集引用?TimberSales C:\Users\kraskell.CDATRIBE2\Documents\Visual Studio 2017\Projects\TimberSales\TimberSales\Controllers\DataEntryController.cs 18 活跃
我的模型的代码是
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TimberSales.Models;
namespace TimberSales.Controllers
{
public class DataEntryController : Controller
{
// GET: DataEntry
public ActionResult Index()
{
TimberSalesEntities db = new TimberSalesEntities();
List<TimberSale> timberSales = db.TimberSales.ToList();
List<DataEntry> dataEntries = timberSales.Select(x => new DataEntry { ContractNumberTimberSales = x.ContractNumber, BidRate = x.VolumesRates.BidRate }).ToList();
return View(dataEntries);
}
}
}
这是Index.cshtml
文件
@model IEnumerable<TimberSales.Models.DataEntry>
@{
ViewBag.Title = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>Index</h2>
<p>
@Html.ActionLink("Create New", "Create")
</p>
<table class="table">
<tr>
<th>
@Html.DisplayNameFor(model => model.ContractNumberTimberSales)
</th>
<th>
@Html.DisplayNameFor(model => model.BidRate)
</th>
</tr>
@foreach (var item in Model) {
<tr>
<td>
@Html.DisplayFor(modelItem => item.ContractNumberTimberSales)
</td>
<td>
@Html.DisplayFor(modelItem => item.BidRate)
</td>
<td>
@Html.ActionLink("Edit", "Edit", new { id=item.ContractNumberTimberSales }) |
@Html.ActionLink("Details", "Details", new { id=item.ContractNumberTimberSales }) |
@Html.ActionLink("Delete", "Delete", new { id=item.ContractNumberTimberSales })
</td>
</tr>
}
</table>
这是DataEntry
类文件:
using System;
using System.Collections.Generic;
using System.Data.Entity;
using System.Linq;
using System.Web;
namespace TimberSales.Models
{
public class DataEntry
{
public string ContractNumberTimberSales { get; set; }
public string LoggingUnit { get; set; }
public string TractNumber { get; set; }
public string Seller { get; set; }
public string ApprovingOfficer { get; set; }
public int NumBids { get; set; }
public decimal TotalTractValue { get; set; }
public decimal TotalTractVolume { get; set; }
public Nullable<decimal> AcreageHarvested { get; set; }
public string SilviculturalTreatment { get; set; }
public string HarvestReason { get; set; }
public Nullable<System.DateTime> BidDate { get; set; }
public Nullable<System.DateTime> CutPayDate { get; set; }
public Nullable<System.DateTime> ContractApprovedDate { get; set; }
public Nullable<System.DateTime> ExtensionApprovedDate { get; set; }
public Nullable<System.DateTime> ExtensionCuttingEndsDate { get; set; }
public Nullable<System.DateTime> ExtentionExpirationDate { get; set; }
public decimal EstimatedBidValue { get; set; }
public decimal TotalAmountReceived { get; set; }
public decimal AdminExpenseDeduction { get; set; }
public decimal AdminExpenseDeductionPercent { get; set; }
public Nullable<System.DateTime> SaleClosureDate { get; set; }
public string Remarks { get; set; }
public decimal ContractAmount { get; set; }
public string ContractorName { get; set; }
public virtual Contractor Contractor { get; set; }
public int Species { get; set; }
public int SawProduct { get; set; }
public Nullable<decimal> EstimatedVolume { get; set; }
public Nullable<decimal> ActualVolume { get; set; }
public Nullable<decimal> BaseRate { get; set; }
public Nullable<decimal> AppraisalRate { get; set; }
public Nullable<decimal> AdvertisedRate { get; set; }
public decimal BidRate { get; set; }
public string ContractNumberVolumeRates { get; set; }
public virtual SawProduct SawProduct1 { get; set; }
public virtual Species Species1 { get; set; }
public virtual TimberSale TimberSale { get; set; }
}
}
TimberSales
对象包含VolumeRates
对象的列表
我是否需要执行select join
才能访问所需的信息,或者是否可以在TimberSales.Select
语句中执行此操作以显示所需的信息,或者是否需要修改index.cshtml
中的@model IEnumerable<TimberSales.Models.DataEntry>
?
为了解决这个问题,您正在尝试将集合推送到小数的 BidRate 字段中。您需要将 VolumeRates 折叠成单个对象,然后使用 FirstOrDefault() 或您需要过滤的任何单个结果来获取 BidRate:
List<DataEntry> dataEntries = timberSales.Select(x => new DataEntry { ContractNumberTimberSales = x.ContractNumber, BidRate = x.VolumesRates.FirstOrDefault().BidRate }).ToList();