我有一個LINQ查詢返回幾行。它有一個連接,因此它包含公司聯繫信息以及詳細記錄(因此每行重複的聯繫信息)。在我的視圖頁面上,我希望只在標題區域顯示聯繫人信息一次,然後循環訪問記錄集以顯示詳細數據。從第一記錄集訪問數據
如何在沒有循環的情況下訪問第一條記錄中的聯繫人數據?
有沒有更好的方法?我避免將所有聯繫人字段填充到ViewBag變量中,因爲我認爲用單個數據庫查詢來獲取它們會更加正確。
BTW獲取代碼塊在這裏工作的訣竅是什麼?我總是點擊代碼按鈕,然後直接從VS粘貼我的代碼。有時候,顏色格式有效,有時不會。
<fieldset>
<legend>Contact Information</legend>
<div class="view-field">
@Html.ValueFor(m => m.CompanyName)
</div>
<div class="view-field">
@Html.ValueFor(m => m.Name)
</div>
<div class="view-field">
@Html.ValueFor(m => m.Address1)
</div>
<div class="view-field">
@Html.ValueFor(m => m.City)
</div>
<div class="view-field">
@Html.ValueFor(m => m.State)
</div>
<div class="view-field">
@Html.ValueFor(m => m.Zip)
</fieldset>
@foreach (var item in Model)
{
<tr>
<td>
@Html.DisplayFor(m => item.StatusDate)
</td>
<td>
@Html.DisplayFor(m => item.StatusName)
</td>
<td>
@Html.DisplayFor(m => item.StatusUserName)
</td>
<td>
@Html.DisplayFor(m => item.StatusNote)
</td>
</tr>
}
編輯 與Boossss思想工作,我想出了這個LINQ和視圖模型,但他們都不太讓我一個可行的解決方案。我得到一個單獨的記錄集,其中包含VendorStatusHistory記錄的子集合,這正是我想要的,它會拋出錯誤,試圖顯示視圖模型的任何屬性「不包含定義...並且不能找到擴展方法...」。我嘗試使用Boossss viewmodel,但它給出了同樣的錯誤。我懷疑我的LINQ查詢已關閉。
已解決 根據Boossss的評論更新。
public StatusHistoryView VendorStatusHistory(String id)
{
Guid pid = Guid.Parse(id);
StatusHistoryView query = _db.VendorProfiles
.Include("VendorStatusHistory")
.Include("StatusCodes")
.Select(s => new StatusHistoryView
{
ProfileID = s.ProfileID,
Name = s.Name,
CompanyName = s.CompanyName,
CompanyDBA = s.CompanyDBA,
Email = s.Email,
Phone = s.Phone,
Website = s.Website,
Address1 = s.Address1,
Address2 = s.Address2,
City = s.City,
State = s.State,
Zip = s.Zip,
VendorStatusHistory = s.VendorStatusHistories
}
)
.Where(x => x.ProfileID == pid).SingleOrDefault();;
return query;
}
public class StatusHistoryView
{
public StatusHistoryView()
{
this.VendorStatusHistory = new HashSet<VendorStatusHistory>();
}
public System.Guid ProfileID { get; set; }
public int StatusID { get; set; }
[Display(Name = "Full Name")]
public string Name { get; set; }
[Display(Name = "Email")]
public string Email { get; set; }
[Display(Name = "Phone")]
public string Phone { get; set; }
[Display(Name = "Website")]
public string Website { get; set; }
[Display(Name = "Company")]
public string CompanyName { get; set; }
[Display(Name = "DBA")]
public string CompanyDBA { get; set; }
[Display(Name = "Address")]
public string Address1 { get; set; }
[Display(Name = "Address (extra)")]
public string Address2 { get; set; }
[Display(Name = "City")]
public string City { get; set; }
[Display(Name = "State")]
public string State { get; set; }
[Display(Name = "Zip Code")]
public string Zip { get; set; }
public virtual ICollection<VendorStatusHistory> VendorStatusHistory { get; set; }
}
和更新後的視圖...
@model VendorProfileIntranet.Models.StatusHistoryView
<table id="VendorTable" width="100%" class="gradeA">
<thead>
@foreach (var item in Model.VendorStatusHistory)
{
<tr>
<th style="width:200px">
@Html.DisplayNameFor(m => item.DateCreated)
</th>
<th>
@Html.DisplayNameFor(m => item.Status)
</th>
<th>
@Html.DisplayNameFor(m => item.UserName)
</th>
<th>
@Html.DisplayNameFor(m => item.Notes)
</th>
</tr>
break;
}
</thead>
<tbody>
@foreach (var item in Model.VendorStatusHistory)
{
<tr>
<td>
@Html.DisplayFor(m => item.StatusDate)
</td>
<td>
@Html.DisplayFor(m => item.StatusName)
</td>
<td>
@Html.DisplayFor(m => item.StatusUserName)
</td>
<td>
@Html.DisplayFor(m => item.StatusNote)
</td>
</tr>
}
</tbody>
</tbody>
</table>
發現一種解決方法是將聯繫信息包裝在foreach中幷包含一個break語句,以便它只循環一次。希望有人能提出一個更優雅的解決方案。 :) – SQLGrinder 2013-05-02 19:59:18