Reputation: 836
I have two tables that have a relationship with each other. I would like to be able to view the information simultaneously in a single view. However, when I try to use an attribute from the VolumeRates table I get the error:
Error CS1061 'ICollection' does not contain a definition for 'BidRate' and no extension method 'BidRate' accepting a first argument of type 'ICollection' could be found (are you missing a using directive or an assembly reference?) TimberSales C:\Users\kraskell.CDATRIBE2\Documents\Visual Studio 2017\Projects\TimberSales\TimberSales\Controllers\DataEntryController.cs 18 Active
The code for my model is
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);
}
}
}
This is the Index.cshtml
file
@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>
And this is the DataEntry
class file:
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; }
}
}
The TimberSales
object contains a list of VolumeRates
objects
Do I need to do a select join
in order to access the information that I need, or can I do this within the TimberSales.Select
statement to display the information that I need, or do I need to modify @model IEnumerable<TimberSales.Models.DataEntry>
in the index.cshtml
?
Upvotes: 0
Views: 52
Reputation: 182
To solve this, you are trying to push a collection into a BidRate field which is a decimal. You need to collapse the VolumeRates into a single object and then get the BidRate, using FirstOrDefault() or whatever single result you need filtered on:
List<DataEntry> dataEntries = timberSales.Select(x => new DataEntry { ContractNumberTimberSales = x.ContractNumber, BidRate = x.VolumesRates.FirstOrDefault().BidRate }).ToList();
Upvotes: 1