>我的问题是以下几点:我有一个使用实体框架的"salesTable"数据网格,我使用以下代码加载数据网格:
salesDataGrid.ItemsSource = _db.salesTables.ToList();
salesTable 的一列是 customerId,它是 customerTable 的外键,现在我想在数据网格中显示 customerName 而不是 customerId,但我不知道怎么做。
销售表如下:
public int saleId { get; set; }
public Nullable<int> customerId { get; set; }
public Nullable<System.DateTime> saleDate { get; set; }
public Nullable<int> invoiceId { get; set; }
public Nullable<decimal> total{ get; set; }
public virtual customerTable customerTable { get; set; }
public virtual fac fac { get; set; }
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Usage", "CA2227:CollectionPropertiesShouldBeReadOnly")]
public virtual ICollection<Sale_Detail> Sale_Detail { get; set; }
请帮忙!
创建此视图模型:
public class GridViewModel
{
public int SaleId { get; set; }
public string CustomerName { get; set; }
//other properties you want
}
然后在查询中使用 GridViewModel:
var result = _db.salesTables.Include(x => x.customerTable)
.Select(x => new GridViewModel()
{
SaleId = x.saleId,
CustomerName = x.customerTable.CustomerName
}).ToList();
最后:
salesDataGrid.ItemsSource = result;