linq 实体框架:基于最后一级子实体的过滤器

fafcakar  于 2023-02-17  发布在  其他
关注(0)|答案(1)|浏览(115)

我有下面的实体类,它们简单地表示关系。
我想从UserPrivilege表中获取已启用标志为真的用户。我无法获取它的LINQ查询。以下是我尝试的代码片段。
由于未定义导航属性,因此以下操作无效
经过一番搜索,我发现我们需要使用投影,但无法得到它。
请帮助执行Linq查询以获取UserPrivilege enabled = true的用户
先谢了

    • 更新**

由于NetworkRoles不是直接Map的属性或Navigation属性,Vince的回答给出了以下错误
System.NotSupportedException:LINQ to Entities中不支持指定的类型成员"NetworkRoles"。仅支持初始值设定项、实体成员和实体导航属性。

hyrbngr7

hyrbngr71#

试试这个

using System;
using System.Collections.Generic;
using System.Linq;

namespace TestThings
{
    class Program
    {
        static void Main(string[] args)
        {
            List<User> users = new List<User>() {
                new User(){
                    Name = "User1",
                    NetworkRoles = new List<NetworkRole>() {
                        new NetworkRole(){
                            Description = "NetworkRole1",
                            NetworkRolePrivileges = new List<NetworkRolePrivilege>() {
                            new NetworkRolePrivilege(){
                                Description = "NetworkRolePrivilege1",
                                UserPrivilege = new UserPrivilege(){
                                    Description = "NetworkRolePrivilege",
                                    Enabled = false
                                }
                            }
                        }
                        }
                    }
                },
                 new User(){
                    Name = "User2",
                    NetworkRoles = new List<NetworkRole>() {
                        new NetworkRole(){
                            Description = "NetworkRole2",
                            NetworkRolePrivileges = new List<NetworkRolePrivilege>() {
                            new NetworkRolePrivilege(){
                                Description = "NetworkRolePrivilege2",
                                UserPrivilege = new UserPrivilege(){
                                    Description = "NetworkRolePrivilege3",
                                    Enabled = true
                                }
                            }
                        }
                        }
                    }
                }
            };

            var _users = users.Where(u=> u.NetworkRoles.SelectMany(x=>x.NetworkRolePrivileges).Where(x=>x.UserPrivilege.Enabled).Any()).ToList();

            foreach(var u in _users)
            {
                Console.WriteLine(u.Name);
            }

            Console.ReadKey();
        }
    }

    class User
    {
        public string Name { get; set; }
        public ICollection<NetworkRole> NetworkRoles { get; set; } = new List<NetworkRole>();
    }

    class NetworkRole
    {
        public string Description { get; set; }
        public ICollection<NetworkRolePrivilege> NetworkRolePrivileges { get; set; }
    }

    class NetworkRolePrivilege
    {
        public string Description { get; set; }
        public UserPrivilege UserPrivilege { get; set; }
    }
    class UserPrivilege
    {
        public string Description { get; set; }
        public bool Enabled { get; set; }
    }
}

相关问题