假设我有一个用EF 4.0创建的模型
用户
角色
每个实体都有一个DeleteDate属性。
我想获得一个特定的用户(使用Name = ...)并让树中填充DeletedDate == null的项目。
这必须以匿名类型投影作为结果,但我不知道如何通过比2更深的层次来实现这一点。
这就是我已经拥有的:
public MyProjection MyCall(string givenName)
{
var result = from s in context.Users
where (s.Name == givenName &&
s.DeletedDate == null)
select new
{
s,
roles = from r in s.Roles
where r.DeletedDate == null
select r
};
var outcome = result.FirstOrDefault();
if (outcome != null)
{
var myProjection = new MyProjection()
{
User = outcome.s,
Roles = outcome.roles
};
return myProjection;
}
return null;
}
答案 0 :(得分:0)
根据您的结构,您可以执行以下操作:
var result = m.Users.Where(u => u.DeletedDate == null)
.Select( u => new
{
u,
roles = u.Roles.Where(r => r.DeletedDate == null)
.Select(r => new
{
r,
permissions = r.Permissions.Where(p => p.DeletedDate == null)
})
}).FirstOrDefault(item => item.u.Name == givenName);
答案 1 :(得分:0)
如果您使用以下内容进行检索:
var result = from s in MyUsers
where s.DeletedDate == null
select new aUser{
Roles = (from r in s.Roles
where r.DeletedDate == null
select r).ToList()
};
然后创建一个TreeView:
TreeView treeView = new TreeView();
然后将TreeView的ItemsSource设置为IEnumerable:
treeView.ItemsSource = result;
然后在TreeView中构建一个HierarchicalDataTemplate来表示你的列表(similar to this或更深入this),然后瞧!