数据库中表如下,现想要获取此表TotalInDrug这一列的总和以及其他表各种Total的总和,最终形成一个柱状图
。。
//主要代码:
HosptialModel h = new HosptialModel();
string sql = "select (select Sum(TotalRegister) from RegisterFinance) as RegisterFee," +
"(select sum(TotalInDrug) from InDrugFinances) as InDrugFee," +
"(select sum( TotalOutDrug) from OutDrugFinances) as OutDrugFee," +
"(select sum(TotalBedFee) from InHospFinances) as InHspFee";
List<FeeKind> result = h.Database.SqlQuery<FeeKind>(sql).ToList();
if (result.Count > 0)
{
chart1.Series["Series1"].Points.AddXY("挂号费", result[0].RegisterFee);
chart1.Series["Series1"].Points.AddXY("入库药品", result[0].InDrugFee);
chart1.Series["Series1"].Points.AddXY("售出药品", result[0].OutDrugFee);
chart1.Series["Series1"].Points.AddXY("住院费", result[0].InHspFee);
chart1.DataSource = result;
}
select语句部分代码:
FeeKind
类中包含了 RegisterFee
, InDrugFee
, OutDrugFee
, 和 InHspFee
等属性
RegisterFee
:从RegisterFinance
表中获取挂号费的总和 (Sum(TotalRegister)
)。InDrugFee
:从InDrugFinances
表中获取入库药品的总费用。OutDrugFee
:从OutDrugFinances
表中获取售出药品的总费用。InHspFee
:从InHospFinances
表中获取住院费用的总和。- SQL 查询通过子查询的方式分别计算每个费用类别的总和,并给每个子查询结果起了别名:
RegisterFee
、InDrugFee
、OutDrugFee
和InHspFee
。 - 通过
ToList()
方法将查询结果转换为一个List<FeeKind>
类型的集合,保存到result
变量
图表部分代码:
-
添加数据到图表
chart1.Series["Series1"].Points.AddXY("挂号费", result[0].RegisterFee)
:将查询结果中的挂号费 (RegisterFee
) 添加到图表的Series1
中,并以"挂号费"
作为横坐标,费用值作为纵坐标。- 类似地,其他费用(如入库药品费、售出药品费和住院费)也被分别添加到图表中的
Series1
。
每次调用
AddXY
方法时,"挂号费"
等是数据点的标签,而对应的费用是数据点的数值。 -
绑定数据源
chart1.DataSource = result;
:将查询结果result
绑定到chart1
的数据源。这意味着图表将使用查询结果作为数据来生成图表。
//FeeKind类
public class FeeKind
{
public int? RegisterFee { get; set; }
public int? InDrugFee { get; set; }
public int? OutDrugFee { get; set; }
public int? InHspFee { get; set; }
}