我正在处理WinForms项目,并且正在使用实体框架。
我曾经使用linq填充sql查询来填充DataGridView,但是现在我需要在sql中使用数据透视表
因此查询为:
SELECT * FROM (SELECT
TP_ROWID,
RE_PLAYER_REFNO,
RE_ST_DATE,
--RE_ED_DATE,
MAX(TP_FULLNAME) TP_NAME,
MAX(HO_NAME) HO_NAME,
SUM(RER_BLOCK_AMOUNT) RER_BLOCK_AMOUNT,
MAX(RE_NIGHT_NUMBER) RE_NIGHT_NUMBER
FROM TOURNAMENT_PERIODS
LEFT JOIN ROOM_RESERVATION ON RR_TP_REFNO = TP_ROWID
LEFT JOIN RESERVED_ROOMS ON RER_M_REFNO = RR_ROWID
JOIN RESERVATIONS ON RE_TP_REFNO = TP_ROWID
JOIN HOTELS ON HO_CODE = RE_HOTEL_REFNO
GROUP BY TP_ROWID, RE_PLAYER_REFNO, RE_ST_DATE, RE_HOTEL_REFNO) t
PIVOT
(
SUM(RE_NIGHT_NUMBER)
FOR [HO_NAME] IN ([A], [MC], [MP], [O], [P], [M])
)AS pvt
在网上搜索后,我发现了以下扩展方法,可以将查询转换为消耗性对象列表。
public static IEnumerable<dynamic> MySql(this DbContext dbContext, string Sql, Dictionary<string, object> Parameters)
{
using (var cmd = dbContext.Database.Connection.CreateCommand())
{
cmd.CommandText = Sql;
if (cmd.Connection.State != ConnectionState.Open)
cmd.Connection.Open();
foreach (KeyValuePair<string, object> param in Parameters)
{
DbParameter dbParameter = cmd.CreateParameter();
dbParameter.ParameterName = param.Key;
dbParameter.Value = param.Value;
cmd.Parameters.Add(dbParameter);
}
using (var dataReader = cmd.ExecuteReader())
{
while (dataReader.Read())
{
var dataRow = GetDataRow(dataReader);
yield return dataRow;
}
}
}
}
但是设置了datagridview之后,我进行了调试,我可以看到消耗性对象和数据源的数量,但是列未填充在gridview中
dataGridView1.DataSource = a.MySql(query, new Dictionary<string, object>()).ToList();
有什么建议吗?