使用 Dapper 在自定义 Poco 对象中返回模型
Returning model in a custom Poco object using Dapper
我正在使用以下查询聚合数据:
var result = Connection.Query<TransactionStatsByUserGrouped>(
@"SELECT usr.*, st.Amount, st.Count
FROM Users usr
RIGHT JOIN (select UserId, sum(Amount) as Amount, sum(Count) Count
FROM (
SELECT User2Id as UserId, sum(Amount) as Amount, count(TransactionId) Count
FROM Transactions
WHERE User1Id = @UserId
GROUP BY User2Id
) t GROUP BY UserId) st
ON st.UserId = usr.UserId
ORDER BY st.Amount DESC",
param: new { UserId = userId },
transaction: Transaction
);
自定义 Poco 对象具有以下结构:
public class TransactionStatsByUserGrouped
{
public User User { get; set; }
public decimal Amount { get; set; }
public int Count { get; set; }
}
其中 User
是一个实际的数据模型,包含以下属性:
public class User
{
public string UserId { get; set; }
public string Email { get; set; }
public int Role { get; set; }
public string Password { get; set; }
// ...
}
我遇到的问题是我在 TransactionStatsByUserGrouped
class:
中得到 User
模型的 null
结果
[
{
"user": null,
"amount": 400.00,
"count": 2
},
{
"user": null,
"amount": 100.00,
"count": 1
}
]
问题似乎在于,自定义 TransactionStatsByUserGrouped
class 将模型用作 属性,而不是将所有模型的属性列为单独的 属性 在 TransactionStatsByUserGrouped
class 中。有解决办法吗?我不想手动映射每个 User
模型 属性。
我想 return 用户的所有属性 + 每个用户的聚合统计信息 - 所有这些都在一个查询中。
使用 .Net Core 2
+ Dapper
+ MySQL
连接器 (MariaDB
)
根据文档你可以试试Multi Mapping
var sql =
@"SELECT usr.*, st.Amount, st.Count
FROM Users usr
RIGHT JOIN (select UserId, sum(Amount) as Amount, sum(Count) Count
FROM (
SELECT User2Id as UserId, sum(Amount) as Amount, count(TransactionId) Count
FROM Transactions
WHERE User1Id = @UserId
GROUP BY User2Id
) t GROUP BY UserId) st
ON st.UserId = usr.UserId
ORDER BY st.Amount DESC";
var result = Connection.Query<TransactionStatsByUserGrouped, User, TransactionStatsByUserGrouped>(
sql,
(group, user) => { group.User = user; return group;},
param: new { UserId = userId },
transaction: Transaction
);
我正在使用以下查询聚合数据:
var result = Connection.Query<TransactionStatsByUserGrouped>(
@"SELECT usr.*, st.Amount, st.Count
FROM Users usr
RIGHT JOIN (select UserId, sum(Amount) as Amount, sum(Count) Count
FROM (
SELECT User2Id as UserId, sum(Amount) as Amount, count(TransactionId) Count
FROM Transactions
WHERE User1Id = @UserId
GROUP BY User2Id
) t GROUP BY UserId) st
ON st.UserId = usr.UserId
ORDER BY st.Amount DESC",
param: new { UserId = userId },
transaction: Transaction
);
自定义 Poco 对象具有以下结构:
public class TransactionStatsByUserGrouped
{
public User User { get; set; }
public decimal Amount { get; set; }
public int Count { get; set; }
}
其中 User
是一个实际的数据模型,包含以下属性:
public class User
{
public string UserId { get; set; }
public string Email { get; set; }
public int Role { get; set; }
public string Password { get; set; }
// ...
}
我遇到的问题是我在 TransactionStatsByUserGrouped
class:
User
模型的 null
结果
[
{
"user": null,
"amount": 400.00,
"count": 2
},
{
"user": null,
"amount": 100.00,
"count": 1
}
]
问题似乎在于,自定义 TransactionStatsByUserGrouped
class 将模型用作 属性,而不是将所有模型的属性列为单独的 属性 在 TransactionStatsByUserGrouped
class 中。有解决办法吗?我不想手动映射每个 User
模型 属性。
我想 return 用户的所有属性 + 每个用户的聚合统计信息 - 所有这些都在一个查询中。
使用.Net Core 2
+ Dapper
+ MySQL
连接器 (MariaDB
)
根据文档你可以试试Multi Mapping
var sql =
@"SELECT usr.*, st.Amount, st.Count
FROM Users usr
RIGHT JOIN (select UserId, sum(Amount) as Amount, sum(Count) Count
FROM (
SELECT User2Id as UserId, sum(Amount) as Amount, count(TransactionId) Count
FROM Transactions
WHERE User1Id = @UserId
GROUP BY User2Id
) t GROUP BY UserId) st
ON st.UserId = usr.UserId
ORDER BY st.Amount DESC";
var result = Connection.Query<TransactionStatsByUserGrouped, User, TransactionStatsByUserGrouped>(
sql,
(group, user) => { group.User = user; return group;},
param: new { UserId = userId },
transaction: Transaction
);