Dapper:如何返回选定的列?

时间:2016-12-23 06:58:08

标签: dapper

from sympy import solve, symbols, tanh
c1 = 1
c2 = 1
c3 = 1
x = symbols('x')
solve(tanh(c1+x*c2)+tanh(c1-x*c2)-c3, x)

有没有人知道如何使用dapper返回特定的列?我想要实现的是仅将public List<Customer> getCustomer() { using (IDbConnection con=DapperConnection()) { string sql = "Select * from Customer"; return con.Query<Customer>(sql).Select(x => new { x.Id, x.LastName }) .ToList(); } } class Customer { public string Id { get; set; } public string FirstName { get; set; } public string LastName { get; set;} } Id作为LastName返回,以便我可以将它们绑定到我的控件。

1 个答案:

答案 0 :(得分:0)

确切地确定您的意思,但肯定您应该返回客户对象而不是匿名类型,或者至少使控件使用的客户对象的较小版本

    public List<Customer> getCustomers()
    {
        using (IDbConnection con = DapperConnection())
        {
            string sql = "Select * from Customer";
            return con.Query<Customer>(sql).ToList();
        }
    }

或者,如果您不喜欢返回完整客户对象的开销

    public List<CustomerBase> getCustomers()
    {
        using (IDbConnection con = DapperConnection())
        {
            string sql = "Select * from Customer";
            return con.Query<CustomerBase>(sql).ToList();
        }
    }

    public class CustomerBase
    {
        public string Id { get; set; }
        public string LastName { get; set; }
    }

    public class Customer: CustomerBase
    {
        public string FirstName { get; set; }
        //Other props...
    }