stackoverfloweth
stackoverfloweth

Reputation: 6917

OrmLite Select with return Type not dynamic

in a simple example like this one

var query = db.From<Employee>()
    .Join<Department>()
    .OrderBy(e => e.Id)
    .Select<Employee, Department>((employee, department) => new { 
        employee.Id, 
        employee.LastName, 
        DepartmentName = department.Name 
    }
);

if I have a class defined for the return type

public class EmployeeWithDept {
    public int Id {get;set}
    public string LastName {get;set}
    public string DepartmentName {get;set}
}

Can I explicitly write my .Select() to return EmployeeWithDept?

var query = db.From<Employee>()
    .Join<Department>()
    .OrderBy(e => e.Id)
    .Select<Employee, Department>((employee, department) => new EmployeeWithDept { 
        employee.Id, 
        employee.LastName, 
        DepartmentName = department.Name 
    }
);

Upvotes: 0

Views: 126

Answers (2)

labilbe
labilbe

Reputation: 3594

Since you are calling db.From<Employee> the returned variable type is SqlExpression<Employee>.

So you can call it like this:

var query = db.From<Employee>()
    .Join<Department>()
    .OrderBy(e => e.Id)
    .Select<Employee, Department>((employee, department) => new { 
        Id = Sql.As(employee.Id, nameof(EmployeeWithDept.Id)), 
        LastName = Sql.As(employee.LastName, nameof(EmployeeWithDept.LastName)), 
        DepartmentName = Sql.As(department.Name, nameof(EmployeeWithDept.DepartmentName))
    }
);
var result = db.Select<EmployeeWithDept>(query);

This should work.

Upvotes: 1

M.Stramm
M.Stramm

Reputation: 1309

According to the docs, yesno. It is very much possible to get out your result POCO as long as you follow the naming conventions or decorated your types with the right attributes.

It should be as easy as:

var query = db.From<Employee>()
    .Join<Department>()
    .OrderBy(e => e.Id);

var esWithDs = db.Select<EmployeeWithDept>(query);

Upvotes: 1

Related Questions