C# 实体框架&x2B;型号+;2个表格+;列表
我对LINQ结果有问题C# 实体框架&x2B;型号+;2个表格+;列表,c#,asp.net-mvc,linq,entity-framework,C#,Asp.net Mvc,Linq,Entity Framework,我对LINQ结果有问题 我的数据库结构 [外键]-->[主键(主键表)] [companyFK]-->[companyID(companyTable)] [billFK]-->[billerID(billerTable)] [attFK]-->[attentedID(attentedTable)] *这是我的发票模型(此模型自动随ADO.NET实体框架提供) ****问题:***正如您在代码和图片中看到的,我只返回外键号。我应该显示行的另一个字段,比如[company ID to c
- 我的数据库结构
public virtual attentedTable attentedTable { get; set; }
public virtual billerTable billerTable { get; set; }
public virtual companyTable companyTable { get; set; }
您可以创建如下视图模型:
public class InvoiceViewModel
{
public int invoiceID { get; set; }
public string companyName { get; set; }
public string currency { get; set; }
public decimal? amt { get; set; }
public DateTime? startDate { get; set; }
public DateTime? endDate { get; set; }
public string billerName { get; set; }
public string attentedName { get; set; }
public string status { get; set; }
}
然后将值指定给ViewModel的每个属性:
using (var db = new PcisDBContext())
{
var retAllInvoicesList= db.invoiceTables.Select(m => new InvoiceViewModel
{
invoiceID = m.invoiceID,
companyName = m.companyTable.companyName,
currency = m.currency,
amt = m.amt,
startDate = m.startDate,
endDate = m.endDate,
billerName = m.billerTable.billerName,
attentedName = m.attentedTable.attentedName,
status = m.status
});
}
return retAllInvoicesList;
最后,您可以使用InvoiceViewModel
ViewModel创建强类型视图
注:
默认情况下启用了延迟加载,如果禁用了延迟加载,则上述查询将不起作用
public class InvoiceViewModel
{
public int invoiceID { get; set; }
public string companyName { get; set; }
public string currency { get; set; }
public decimal? amt { get; set; }
public DateTime? startDate { get; set; }
public DateTime? endDate { get; set; }
public string billerName { get; set; }
public string attentedName { get; set; }
public string status { get; set; }
}
using (var db = new PcisDBContext())
{
var retAllInvoicesList= db.invoiceTables.Select(m => new InvoiceViewModel
{
invoiceID = m.invoiceID,
companyName = m.companyTable.companyName,
currency = m.currency,
amt = m.amt,
startDate = m.startDate,
endDate = m.endDate,
billerName = m.billerTable.billerName,
attentedName = m.attentedTable.attentedName,
status = m.status
});
}
return retAllInvoicesList;