How can I find a second property with a duplicate name from a dynamic object?

dapper

Question

Using Dapper I have a result set which contains two SQL columns with the same name. How do I read the values?

Both the User and Skill table have a column called Name.

An example of my code:

var query = _connection.Query(@"
    select u.*, s.* 
    from [User] u 
    left join Skill s ON s.UserID = u.UserID
    where Username=@Username", 
    new { Username }
);

Accepted Answer

There won't be two name properties. In my short investigation the result contained the first name but I'm sure this is not guaranteed.

To get both name's you will need to alias one or both columns in your sql:

var query = _connection.Query(@"
    select u.name as user_name, s.name as skill_name 
    from [User] u 
    left join Skill s ON s.UserID = u.UserID
    where Username=@Username", 
    new { Username }
);



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why