我的问题如下:我有一个使用实体框架的“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; }请帮忙!
1 回答
慕勒3428872
TA贡献1848条经验 获得超6个赞
创建这个视图模型:
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;
- 1 回答
- 0 关注
- 49 浏览
添加回答
举报
0/150
提交
取消