2

I think I am following the solution from this post but I am not sure why I am getting this error:No mapping exists from object type System.Collections.Generic.List`1 to a known managed provider native type

This is my code:

public virtual IEnumerable<MyModel> QueryAllById(ICollection<string> ids)
{
    var sql = mySelectQuery + @"
                    WHERE SomeId IN @Ids                            
            ";            
    return Db.Query<MyModel>(sql, new { Ids = new[] { ids } });
}
Andi Keikha
  • 1,246
  • 2
  • 16
  • 37
  • 1
    You have an array of collections, try turning your collection into an array instead. – Lasse V. Karlsen Jun 06 '18 at 13:24
  • @LasseVågsætherKarlsen So I changed it to " new { Ids = new[] {ids.ToArray()} } ", No I gt this error: No mapping exists from object type System.String[] to a known managed provider native type. – Andi Keikha Jun 06 '18 at 13:29
  • 1
    Sorry, I was being unclear, just do `new { Ids = ids.ToArray() }` – Lasse V. Karlsen Jun 06 '18 at 13:31
  • 1
    @LasseVågsætherKarlsen Oh Found it!! I have an array of collection, damn. It took me an hour. I think I should get rid of the question. I made a silly mistake. – Andi Keikha Jun 06 '18 at 13:33
  • @LasseVågsætherKarlsen If you think the question is okay, I can give you the credit by your response, but if you think that it might distract readers from the actual point, I can vote to delete it. – Andi Keikha Jun 06 '18 at 13:36

1 Answers1

1

You can convert ICollection to Array.

public virtual IEnumerable<MyModel> QueryAllById(ICollection<string> ids)
{
    var sql = mySelectQuery + @"
                    WHERE SomeId IN @Ids                            
            ";            
    return Db.Query<MyModel>(sql, new { Ids = ids.ToArray() });
}
  • This is a exactly copy of @LasseVågsætherKarlsen Comment. – Smartis has left SO again Jun 06 '18 at 13:58
  • 1
    Yes, but that is completely OK! I didn't monitor the question and didn't intend to post it as an answer, so he did it instead, completely OK with me. – Lasse V. Karlsen Jun 06 '18 at 15:41
  • 1
    @LasseVågsætherKarlsen Thanks mate for your kind words. I am really sorry. I got late to post the answer. Smartis, When I saw the question, there was only one answer. after that I first created the same scenario in My project and then I post it after testing. and while posting I didn't noticed that.Apologies to All. Next time before posting an answer I'll check. – Pranil Dukare Jun 08 '18 at 05:50