1

I'm using Dapper, and trying to get a collection of Players using the following query:

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});
}

The Get method looks like this:

public ICollection<T> Get<T>(string sql, ICollection<int> ids)
{
    using (var connection = new SqlConnection(ConnectionString()))
    {
        return connection.Query<T>(sql, new { ids }).ToICollection();
    }
}

And, ToICollection looks like this:

public static ICollection<T> ToICollection<T>(this IEnumerable<T> iEnumerable)
        {
            var icollection = iEnumerable as ICollection<T>;
            return icollection ?? iEnumerable.ToArray();
        }

This is the error I'm getting on connection.Query:

Additional information: Incorrect syntax near '@Ids'.

If I run this query in SQL Management Studio, it works:

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)

I can't really figure out where my error is, as per my understanding the query generated by Dapper should be the same as the one I write myself in SQLMS?

spersson
  • 538
  • 1
  • 8
  • 19
  • `IN (13)` and `IN @Ids` arent the same... try `IN (@Ids)` – crthompson May 08 '15 at 17:38
  • @paqogomez According to the [Dapper website](https://github.com/StackExchange/dapper-dot-net) it will put the parentheses in for you. – juharr May 08 '15 at 17:42
  • Where are you setting a value for and passing the @Ids parameter? – waltmagic May 08 '15 at 17:44
  • @waltmagic It's the `new { ids}` part. Dapper should see that `ids` is an `IEnumerable` and replace `@Ids` with a list of parameters. See the link in my comment above. – juharr May 08 '15 at 17:46
  • What type of exception is it and what is the stack trace? – juharr May 08 '15 at 17:47
  • 1
    I have little knowledge of dapper, but c# is case-sensative, so I thinking `@Ids` != `ids`.. you're creating an object (`new { ids }`) with what I presume is resolved to an object with a property called `ids` (`ICollection ids`).. – Brett Caswell May 08 '15 at 17:48
  • Duplicate? http://stackoverflow.com/questions/8388093/select-from-x-where-id-in-with-dapper-orm – Steve May 08 '15 at 17:48
  • 3
    Also I'm pretty sure Dapper is case insensitive, but have you tried `new { Ids = ids }` – juharr May 08 '15 at 17:49
  • I'm thinking @juharr is correct, I presume you could also do `new { Ids = ids.ToArray() }` as well. you could also do `System.Linq.Expression.Expression> WhereClause = id => id > 0; new {Ids = ids.Where(WhereClause).ToArray()}` if you wanted to add a condition. – Brett Caswell May 08 '15 at 17:58
  • I found this [bug fix](https://code.google.com/p/dapper-dot-net/issues/detail?id=7) about case sensitivity when setting the results. Not sure if the same was done for the input parameters. – juharr May 08 '15 at 18:00
  • @juharr I thought it was case insensitive, but I tried your suggestion (kind of). I changed "@Ids" in the query to "@ids" and it worked. Weird though, because when I just use a single int as parameter "@Id" works fine even though I have "id" everywhere else. So please post an answer and I'll accept it :) – spersson May 08 '15 at 18:22
  • 2
    K, this *probably* counts as a bug, then. I'll see if I can fix the case sensitivity in that one scenario. – Marc Gravell May 08 '15 at 20:15

0 Answers0