Dapper - 如何使用动态对象

时间:2013-11-05 16:56:30

标签: c# dynamic dapper

我正在使用Dapper从SQL查询并拥有动态查询:

var returns = conn.Query(dynamicQuery);

当我循环查看结果时,我想知道我正在处理的日期类型是什么,所以我尝试执行以下操作:

foreach (var result in results)
{
    MessageBox.Show(result.GetType().ToString());
}

MessageBox的错误始终失败,错误为Cannot perform runtime binding on a null reference

如果我改用它:

var returns = conn.Query<object>(dynamicQuery);

然后该命令有效,但它给了我一个Dapper.SqlMapper+DapperRow对象类型。

如何找到dynamic变量的类型?

3 个答案:

答案 0 :(得分:19)

使用动态api,您应该知道列的形状,即

foreach(dynamic row in query) {
    int id = row.Id;
    //...
}

但是,如果事情不太明确,每一行也会实现IDictionary<string, object>:所以强制转换为。{/ p>

或者,如果(注释)您知道有一个类型日期时间的单个单元格:

var when = conn.Query<DateTime>(...).Single();

答案 1 :(得分:-3)

int RecCount = ((dynamic)res[0]).RecCount;

答案 2 :(得分:-3)

Friend Function GetDepartment(ByVal DateFrom As String, ByVal DateTo As String)
    Dim xSQL As New System.Text.StringBuilder
    xSQL.AppendLine("SELECT SUM(a.Quantity ) AS quantity, ")
    xSQL.AppendLine("    SUM(a.TotalAmount ) AS totalamount, ")
    xSQL.AppendLine("    a.ProductID, ")
    xSQL.AppendLine("    c.DepartmentID, ")
    xSQL.AppendLine("    a.LongName, ")
    xSQL.AppendLine("    c.Department ")
    xSQL.AppendLine("FROM Transaction01Details a ")
    xSQL.AppendLine("    INNER JOIN Product00header b ON a.ProductID = b.ProductID ")
    xSQL.AppendLine("    INNER JOIN Department00header c ON b.DepartmentID = c.DepartmentID ")
    xSQL.AppendLine("WHERE (a.Tag4 = 'i') ")
    xSQL.AppendLine("    AND (a.TransDate BETWEEN @Date1 AND @Date2) ")
    xSQL.AppendLine("GROUP BY a.ProductID ")
    xSQL.AppendLine("ORDER BY a.LongName ")

    ' Lambda Expression
    Dim lambda = cn.Query(xSQL.ToString, New With {.Date1 = DateFrom, .Date2 = DateTo}).Select(Function(p) New With {.ProductID = CStr(p.ProductID), _
                                                                                    .DepartmentID = CStr(p.DepartmentID), _
                                                                                    .LongName = CStr(p.LongName), _
                                                                                    .Department = CStr(p.Department), _
                                                                                    .Quantity = CDec(p.Quantity), _
                                                                                    .TotalAmount = CDec(p.TotalAmount)}).ToList

    ' Linq Expression
    Dim linq = (From p In cn.Query(xSQL.ToString, New With {.Date1 = DateFrom, .Date2 = DateTo})
                Select New With {.ProductID = CStr(p.ProductID), ' Note, All p.Object is also dynamic
                                 .DepartmentID = CStr(p.DepartmentID), 
                                 .LongName = CStr(p.LongName),
                                 .Department = CStr(p.Department),
                                 .Quantity = CDec(p.Quantity),
                                 .TotalAmount = CDec(p.TotalAmount)}).ToList


    ' in linq, no need to declare function and also no need to put this  --- >  _ to continue the statement


End Function

你可以通过Visual Basic实现动态映射..我给了鱼..轮到你做饭了