Dapper passing parameters to a multi-mapper

时间:2016-02-12 19:22:50

标签: dapper

Using a single table query I get how you can do this:

var sql = @"SELECT NAME, ID, COUNTY
                FROM INFO_NAME
                WHERE (NAME LIKE @Name) ORDER BY NAME, ID ASC";

IEnumerable<NameView> entrys = sqlConnection.Query<NameView>(sql, new { Name = "%" + name + "%" });

Now I'm using the multi-mapping and not quite sure how to pass in a parameter.

var sql = @"SELECT NAME, a.ID, 
        b.ID, ADDRESS
        FROM INFO_NAME_NEW a 
        LEFT OUTER JOIN  INFO_ADDRESS b on a.ID = b.ID
        WHERE (NAME LIKE @TName) ORDER BY NAME, a.ID ASC";

var entrys = sqlConnection.Query<NameView, AddressView, NameView>(sql,
    (a, b) =>
    {
        a.AddressView = b;

        return a;
    },
    splitOn: "ID").AsQueryable();           

I was testing the DynamicParameters (But it was talked about in stored procedure so not sure if this is even the right direction):

var param = new DynamicParameters();
param.Add("@Name", "%" + name + "%");

I tried:

(a, b) =>
{
    a.Name =  "%" + name + "%";
    a.AddressView = b;

    return a;
},

But that the results shows the name as "%somename%". Any suggestions?

Update 1

Thanks Marc, your advice solved my problem. So now my code looks as follows:

var entrys = sqlConnection.Query<NameView, AddressView, NameView>(sql,
    (a, b) =>
    {
        a.AddressView = b;

        return a;
    },
    new { Name = "%" + name + "%" },
    splitOn: "ID").AsQueryable();   

And is good

0 个答案:

没有答案