C# 如何使用实体框架包含另一个要查询的相关对象,并在DataGridView中显示它?
我不知道如何正确表述这个问题,但我正在编写一个销售系统,我的问题非常简单,但我对使用WindowsForm的DataGridView没有很好的经验。我想在DataGridView中显示每笔销售的总价,但要获得总价,我需要在相关表格中对每种产品进行汇总 这是我的销售表: 我正试图得到每笔销售的总价格,并与preciottal相加 这是我的代码:C# 如何使用实体框架包含另一个要查询的相关对象,并在DataGridView中显示它?,c#,entity-framework,windows-forms-designer,C#,Entity Framework,Windows Forms Designer,我不知道如何正确表述这个问题,但我正在编写一个销售系统,我的问题非常简单,但我对使用WindowsForm的DataGridView没有很好的经验。我想在DataGridView中显示每笔销售的总价,但要获得总价,我需要在相关表格中对每种产品进行汇总 这是我的销售表: 我正试图得到每笔销售的总价格,并与preciottal相加 这是我的代码: public IList<Factura> GetListVentas() { return _context
public IList<Factura> GetListVentas()
{
return _context.Facturas
.OrderByDescending(a => a.FechaVenta)
.ToList();
}
public IList GetListVentas()
{
return\u context.Facturas
.OrderByDescending(a=>a.FechaVenta)
.ToList();
}
在DataGridview中显示项目的我的windows窗体代码:
BindingSource source = new BindingSource();
var ventas = _facturasRepository.GetListVentas();
source.DataSource = ventas;
Listaventas.DataSource = typeof(List<>);
Listaventas.DataSource = source;
BindingSource=newbindingsource();
var ventas=_facturasRepository.GetListVentas();
source.DataSource=ventas;
Listaventas.DataSource=typeof(列表);
Listaventas.DataSource=source;
我所尝试的:
public IList<Factura> GetListVentas()
{
return _context.Facturas
.Include(a => a.DetalleFacturas.Sum(b => b.PrecioTotal))
.OrderByDescending(a => a.FechaVenta)
.ToList();
}
public IList GetListVentas()
{
return\u context.Facturas
.包括(a=>a.detalfacturas.Sum(b=>b.preciotottal))
.OrderByDescending(a=>a.FechaVenta)
.ToList();
}
但我得到了这个错误:
System.ArgumentException:'包含路径表达式必须引用在类型上定义的导航属性。使用虚线路径作为参考导航属性,选择运算符作为集合导航属性
public class FacturaDto{
公共十进制总数{get;set;}
...
}
公共IListGetListVentas()
{
return\u context.Facturas
.Include(a=>a.detalefacturas).OrderByDescending(a=>a.FechaVenta).选择(x=>newfacturadto{Total=x.detalefacturas.Sum(b=>b.preciotottal),…})
.ToList();
}
您需要创建一个dto对象,该对象将具有Factura属性和Total amount。然后在查询中选择。类似于return _context.Facturas.Include(a=>a.DetalleFacyuras).OrderByDesending(..).Select(x=>newfacurasumarydto{Total=x.DetallaFacturas.sum(…),…});
public class FacturaDto{
public decimal Total {get;set;}
...
}
public IList<FacturaDto>GetListVentas()
{
return _context.Facturas
.Include(a => a.DetalleFacturas). OrderByDescending(a => a.FechaVenta).Select(x=> new FacturaDto{Total = x.DetalleFacturas.Sum(b => b.PrecioTotal), ...})
.ToList();
}