我正在使用Dapper,并尝试使用以下查询获取一组玩家:
public ICollection<Player> GetPlayersWithPointsInMatch(int id)
{
const string sql =
@"SELECT
p.Id,
p.FirstName,
p.LastName,
pmr.Goals,
pmr.Assists
FROM Players p
LEFT JOIN PlayerMatchStats pmr ON p.Id = pmr.PlayerId
WHERE pmr.MatchId IN @Ids
AND (pmr.Goals > 0 OR pmr.Assists > 0)";
return Get<Player>(sql, new[]{id});
}
Get方法如下所示:
public ICollection<T> Get<T>(string sql, ICollection<int> ids)
{
using (var connection = new SqlConnection(ConnectionString()))
{
return connection.Query<T>(sql, new { ids }).ToICollection();
}
}
而且,ToICollection看起来像这样:
public static ICollection<T> ToICollection<T>(this IEnumerable<T> iEnumerable)
{
var icollection = iEnumerable as ICollection<T>;
return icollection ?? iEnumerable.ToArray();
}
这是我在连接上遇到的错误。查询:
其他信息:&#39; @ Ids&#39;附近的语法不正确。
如果我在SQL Management Studio中运行此查询,则可以正常运行:
SELECT
p.Id,
p.FirstName,
p.LastName,
pmr.Goals,
pmr.Assists
FROM Players p
LEFT JOIN PlayerMatchStats pmr ON p.Id = pmr.PlayerId
WHERE pmr.MatchId IN (13)
AND (pmr.Goals > 0 OR pmr.Assists > 0)
我无法弄清楚我的错误在哪里,根据我的理解,Dapper生成的查询应该与我在SQLMS中编写的查询相同?