1
這是我的數據庫看起來像我的條碼是由WhID
public ActionResult Details(int? id)
{
if (id == null)
{
return new HttpStatusCodeResult(HttpStatusCode.BadRequest);
}
var viewModel =
(from Wh in db.Werehouses
join WhK in db.WerehouseKeys on Wh.WhID equals WhK.WhID
join Itm in db.Items on Wh.WhID equals Itm.WhID
where Wh.WhID == id
select new WerehouseViewModel { Werehouse = Wh, WerehouseKey = WhK, Item = Itm });
if (viewModel == null)
{
return HttpNotFound();
}
return View(viewModel);
}
會從數據庫中的所有數據,但是,當我想表明它在查看它顯示重複的鍵(例如鍵1,鍵2,鍵3,鍵1,鍵2,鍵3等)和項目(項目1,項目2,項目1,項目2等)。我如何刪除查詢中的重複項?
這是我的ViewModel
public class WerehouseViewModel
{
public Werehouse Werehouse { get; set; }
public WerehouseKey WerehouseKey { get; set; }
public Item Item { get; set; }
}
嘛,要加入'Werehouses' (1)到2個不同的(很多)實體,所以結果是相關的'WerehouseKeys'和'Items'記錄的笛卡爾積。您可能需要考慮更改視圖模型並使用組聯接。 –
非常感謝您的建議,請問您如何編輯它? – Cezar
我會讓'WerehouseViewModel'成爲這樣的'Public Werehouse Werehouse {get;組; } public List WerehouseKeys {get;組; } public List - Items {get;組; }' –