Casey Crookston
Casey Crookston

Reputation: 13945

Dapper w/ SIMPLECrud: GetList with WHERE behaving like LIKE instead of =

I have this method:

    public static IEnumerable<T> GetList(string where, Dictionary<string, object> parameters)
    {
        IEnumerable<T> entities;
        using (var connection = OpenConnection())
        {
            entities = connection.GetList<T>(where, new DynamicParameters(parameters));
        }
        return entities;
    }

And I call it like such:

string publicID = "463EC1EE-8AAB-4ABA-9B39-132BC8D3236E"
Dictionary<string, object> parameters = new Dictionary<string, object>();
parameters.Add("@APIPublicID", publicID);
var apiUsers = Repository<APIUsers>.GetList("WHERE APIPublicID = @APIPublicID", parameters).ToList();

The GetList() method calls the SIMPLECrud .dll that is a wrapper over Dapper.

That works beautifully. But here's the really weird thing. If I add some extra letters or numbers on the end of the guid publicID, it STILL works:

Instead of:

string publicID = "463EC1EE-8AAB-4ABA-9B39-132BC8D3236E"

I do...

string publicID = "463EC1EE-8AAB-4ABA-9B39-132BC8D3236EABCDEFG"
.... // rest of the method as before

I get the exact same results. If I make the guid shorter, or if I change characters inside of it, then it behaves as expected.

What am I doing wrong here?

Upvotes: 2

Views: 1294

Answers (1)

Evk
Evk

Reputation: 101443

I think it's not related to dapper or SIMPLECrud but how SQL Server converts strings to uniqueidentifier. I assume that you use SQL Server but if not - probably your database behaves in similar way.

When casting string to uniqueidentifier, SQL server will just ignore exsessive characters:

select cast('463EC1EE-8AAB-4ABA-9B39-132BC8D3236EABCDEFG' as uniqueidentifier)
-- no errors, returns 463EC1EE-8AAB-4ABA-9B39-132BC8D3236E

That means if APIPublicID column in your example is of type uniqueidentifier, the following queries will behave the same:

select * from MyTable WHERE APIPublicID = '463EC1EE-8AAB-4ABA-9B39-132BC8D3236E'
select * from MyTable WHERE APIPublicID = '463EC1EE-8AAB-4ABA-9B39-132BC8D3236EABCDEFG'

Because to compare, they have to be of the same type, so your string is converted to uniqueidentifier, ignoring excessive part (ABCDEFG).

Upvotes: 2

Related Questions