我如何 return 使用 QueryOver 的通用列表
How do I return a generic list with QueryOver
我正在尝试使用 queryover 做一个简单的 select 并获得一个通用列表。
我只想从 table 中获取 username
、email
和 firstname
并忽略其余部分。
这是我的代码:
public IList<Users> GetAllByRole(string role)
{
var users= this.Session.QueryOver<Users>()
.where(f => f.role == role)
.select(f => f.username)
.select(f => f.email)
.select(f => f.firstname)
.list<Users>();
return users;
}
错误:
The value \"x\" is not of type \"Model.Poco.Entities.Users\" and cannot be used in this generic collection.\r\nParameter name: value
SQL 查询应该是这样的
SELECT [username]
,[email]
,[firstname]
FROM [MyDB].[dbo].[MyTable]
WHERE [role] = 'admin'
我也试过类似的方法
IList<Users> users = this.Session.QueryOver<Users>()
.Where(p => p.role == role)
.SelectList(list => list
.Select(p => p.username)
.Select(p => p.email)
.Select(p => p.firstname)
)
.List<Users>();
return users;
错误:
The value \"System.Object[]\" is not of type \"Poco.Entities.Users\" and cannot be used in this generic collection.\r\nParameter name: value
我们需要添加 1) aliases 到每列并使用 2) transformer:
// Users u - will serve as an alias source below
Users u = null;
IList<Users> users = this.Session.QueryOver<Users>()
.Where(f => f.role == role)
.SelectList(list => list // here we set the alias
.Select(p => p.username) .WithAlias(() => u.username)
.Select(p => p.email) .WithAlias(() => u.email)
.Select(p => p.firstname).WithAlias(() => u.firstname)
)
// here we Transform result with the aliases into Users
.TransformUsing(Transformers.AliasToBean<Users>())
.List<Users>();
我正在尝试使用 queryover 做一个简单的 select 并获得一个通用列表。
我只想从 table 中获取 username
、email
和 firstname
并忽略其余部分。
这是我的代码:
public IList<Users> GetAllByRole(string role)
{
var users= this.Session.QueryOver<Users>()
.where(f => f.role == role)
.select(f => f.username)
.select(f => f.email)
.select(f => f.firstname)
.list<Users>();
return users;
}
错误:
The value \"x\" is not of type \"Model.Poco.Entities.Users\" and cannot be used in this generic collection.\r\nParameter name: value
SQL 查询应该是这样的
SELECT [username]
,[email]
,[firstname]
FROM [MyDB].[dbo].[MyTable]
WHERE [role] = 'admin'
我也试过类似的方法
IList<Users> users = this.Session.QueryOver<Users>()
.Where(p => p.role == role)
.SelectList(list => list
.Select(p => p.username)
.Select(p => p.email)
.Select(p => p.firstname)
)
.List<Users>();
return users;
错误:
The value \"System.Object[]\" is not of type \"Poco.Entities.Users\" and cannot be used in this generic collection.\r\nParameter name: value
我们需要添加 1) aliases 到每列并使用 2) transformer:
// Users u - will serve as an alias source below
Users u = null;
IList<Users> users = this.Session.QueryOver<Users>()
.Where(f => f.role == role)
.SelectList(list => list // here we set the alias
.Select(p => p.username) .WithAlias(() => u.username)
.Select(p => p.email) .WithAlias(() => u.email)
.Select(p => p.firstname).WithAlias(() => u.firstname)
)
// here we Transform result with the aliases into Users
.TransformUsing(Transformers.AliasToBean<Users>())
.List<Users>();