c# dapper error when parameter is list<int> 对象类型 <>f__AnonymousType20`1[[System.Int32[] 不存在映射

c# dapper error when parameter is list<int> No mapping exists from object type <>f__AnonymousType20`1[[System.Int32[]

string sqlQuery = "SELECT SellingPrice, MarkupPercent, MarkupAmount FROM ProfitMargins WHERE QuoteId in @QuoteId";
var profitMargin = await ctx.Database.SqlQuery<dynamic>(sqlQuery, 
    new { QuoteId = new[] { 1, 2, 3, 4, 5 } }
//String.Join(", ", QuoteIds.ToArray()))).ToListAsync();

有人能看出我做错了什么吗?

No mapping exists from object type <>f__AnonymousType20`1[[System.Int32[], mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=]] to a known managed provider native type.

我从这个 post 得到了这个想法:SELECT * FROM X WHERE id IN (…) with Dapper ORM 回答者:@LukeH

更新:

我需要将它返回到列表中。看看我的整个功能,我已经根据@JFM 的答案 post 更改了代码,但现在无法添加 .ToListAsync

@JFM

public static async Task<List<dynamic>> GetProfitMargin(List<int> QuoteIds)
{
    using (var conn = new SqlConnection(new MYContext().Database.Connection.ConnectionString))
    {   
       string sqlQuery = "SELECT SellingPrice, MarkupPercent, MarkupAmount FROM ProfitMargins WHERE QuoteId in @QuoteId";
        {
            var profitMargin =  conn.Query<dynamic>(sqlQuery
               , new { QuoteId = new[] { 1, 2, 3, 4, 5 } }).ToListAsync());                    
        }

使用 Dapper 查询和映射动态对我来说效果很好:

string sqlQuery = "SELECT SellingPrice, MarkupPercent, MarkupAmount FROM ProfitMargins WHERE QuoteId in @QuoteId";

using(var conn = new SqlConnection(myConnString)
{
    var profitMargin = conn.Query<dynamic>(sqlQuery
       , new { QuoteId = new[] { 1, 2, 3, 4, 5 } });

}
public static async Task<IEnumerable<dynamic>> GetProfitMargin(List<int> QuoteIds)
    {

        using (var conn = new SqlConnection(new MYContext().Database.Connection.ConnectionString))
        {   
           string sqlQuery = "SELECT SellingPrice, MarkupPercent, MarkupAmount FROM ProfitMargins WHERE QuoteId in @QuoteId";
            {
                IEnumerable<dynamic> profitMargin =  await conn.QueryAsync<dynamic>(sqlQuery
                   , new { QuoteId = new[] { 1, 2, 3, 4, 5 } });                    
            }

如果您不将其映射到列表或数组,默认情况下它将是一个 IEnuerable。

不能 100% 确定问题出在哪里,但下面是您如何使用 Dynamics 的示例:

    [Test]
    public void TestDynamicsTest()
    {
        var query = @"select 1 as 'Foo', 2 as 'Bar' union all select 3 as 'Foo', 4 as 'Bar'";

        var result = _connection.Query<dynamic>(query);

        Assert.That(result.Count(), Is.EqualTo(2));
        Assert.True(result.Select(x => x.Foo == 1).First());
        Assert.True(result.Select(x => x.Bar == 4).Last());
    }

更新

public class Person
{
    public int Id { get; set; }
    public string Name { get; set; }
}

var query = @"select 1 as 'Id', 'Foo' as 'Name' union all select 1 as 'Id', 'Bar' as 'Name'";
var result = _connection.Query<Person>(query);

foreach (var person in result)
{
    var output = string.Format("Id: {0}, Name: {1}", person.Id, person.Name);
}

有关更多示例,请查看 Dapper docs