当我尝试在创建另一个POCO的LINQ查询中使用POCO创建一个通用List时,我遇到了问题:
var user =
from u in ctx.users
select new User
{
id = u.UserID,
name = u.Name,
roles = u.Roles.ToList()
};
我创建了User和Role类。如果我将User中的'roles'定义为:
public List<Roles> roles;
我没问题。这是使用Entities Roles对象,我宁愿使用自己的角色POCO:
public List<Role> roles;
然而,这会引发以下错误:
Error 3 Cannot convert lambda expression to type 'string' because it is not a delegate type
我究竟如何使用POCO而不是Entity对象?
答案 0 :(得分:2)
var user =
from u in ctx.users
select new User
{
id = u.UserID,
name = u.Name,
roles = from r in u.Roles
select new Role
{
...
}
};
答案 1 :(得分:0)
List<Data.User> userEntities = ctx.Users.Include("Roles").ToList();
List<POCO.User> users =
from u in userEntities
select new User()
{
id = u.UserId
name = u.Name
roles =
(
from r in Roles
select new Role()
{
}
).ToList()
}
答案 2 :(得分:-1)
你有一个命名空间:
using System.Linq;