导航查询 dto 返回

SqlSugar 处理完成
6 369
该叫什么 91 发布于2周前
悬赏:0 飞吻

(1)var list3 = db.Queryable<SysUser>()

    .Includes(x => x.BList)

    .ToList();


(2)var query5 = db.Queryable<SysUser>().Includes(x => x.BList).Select(x => new

{

    name = x.KeyUser,

    BList = x.BList.ToList(),

}).ToJson();


这样select 有错吗  



 BList 永远为Null  但是 我如果注释  BList = x.BList.ToList(),  只留name也可以显示出来



1的输出是完全正确的 Blist也可以显示出来


为什么内 


老板


/// <summary>

/// 用户表

///</summary>

[SugarTable("SysUser")]

public class SysUser

{


    /// <summary>

    /// 自增字段

    ///</summary>

    [SugarColumn(ColumnName = "Id_User", IsPrimaryKey = true, IsIdentity = true)]

    public int IdUser { get; set; }


    /// <summary>

    /// 表主键

    /// 默认值: (newsequentialid())

    ///</summary>

    [SugarColumn(ColumnName = "KeyUser")]

    public string? KeyUser { get; set; }


    /// <summary>

    /// 导航查询中间表

    /// </summary>


    [Navigate(typeof(SysUserRole), nameof(SysUserRole.KeyUser), nameof(SysUserRole.KeyRole), nameof(SysUser.KeyUser), nameof(SysRole.KeyRole))]

    public List<SysRole> BList { get; set; }

 


热忱回答6

  • 0 回复
  • 91 91 VIP0
    2周前
    using SqlSugar;
    
    namespace sqlsugartest
    {
        internal class Program
        {
            static void Main(string[] args)
            {
    
               
                var db = new SqlSugarClient(new ConnectionConfig
                {
                    ConnectionString = "server=localhost;Database=Sqlsugardb1;Uid=sa;Pwd=791554930;Encrypt=True;TrustServerCertificate=True",
                    DbType = DbType.SqlServer,
                    IsAutoCloseConnection = true
                });
                db.DbMaintenance.CreateDatabase();
                db.DbMaintenance.TruncateTable<Users>();
                db.DbMaintenance.TruncateTable<UserRoles>();
                db.DbMaintenance.TruncateTable<Roles>();
              
                db.CodeFirst.SetStringDefaultLength(200)
     .InitTables(typeof(Users), typeof(UserRoles),typeof(Roles));
    
                Guid Keyuser = Guid.NewGuid();
                Guid Keyrole = Guid.NewGuid();
    
                db.Insertable(new Users()
                {
                   UserName = "admin",
                   KeyUser = Keyuser
                }).ExecuteCommand();
    
                db.Insertable(new Roles()
                {
                   RoleName = "超级管理员",
                   KeyRole = Keyrole
                }).ExecuteCommand();
    
                db.Insertable(new UserRoles()
                {
                   KeyUser = Keyuser,
                   KeyRole = Keyrole
                }).ExecuteCommand();
    
    
    
    
                ///是可以出来Roleslist的
                var list1 = db.Queryable<Users>()
                   .Includes(x => x.Roleslist).ToJson();
    
    
    
                //blist就是为空
                var list2 = db.Queryable<Users>()
                    .Includes(x => x.Roleslist)
                    .Select( x=>new 
                    { a=x.UserName,
                      blist=x.Roleslist //如果我注释掉blist 那么a=x.UserName也会正常的出现
                    })
                    .ToJson();
                Console.WriteLine(list1);
                Console.ReadKey();
            }
        }
    }
    
    
    public class Users
    {
        [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
        public int UserId { get; set; }
        public string UserName { get; set; }
    
    
        public Guid KeyUser { get; set; }
    
        [Navigate(typeof(UserRoles), nameof(UserRoles.KeyUser), nameof(UserRoles.KeyRole), nameof(KeyUser), nameof(Roles.KeyRole))]//名字换
        public List<Roles> Roleslist { get; set; }
    
    }
    public class UserRoles
    {
        [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
        public int UserRoleId { get; set; }
        public Guid KeyRole { get; set; }
    
    
        public Guid KeyUser { get; set; }
    
    }
    public class Roles
    {
        [SugarColumn(IsIdentity = true, IsPrimaryKey = true)]
        public int RoleId { get; set; }
        public string RoleName { get; set; }
    
    
        public Guid KeyRole { get; set; }
    
    }

    @fate sta

    0 回复
  • 我看一下

    0 回复
  • 91 91 VIP0
    1周前

    @fate sta:  好的  你看下老板

    0 回复
  • 91 91 VIP0
    1周前
      var list2 = db.Queryable<Users>()
    .Includes(x => x.Roleslist)
    .ToList() 
    .Select(x => new
    {
        a = x.UserName,
        blist = x.Roleslist
    })
    .ToList();

    这样写就可以

    0 回复
  • 91 91 VIP0
    1周前
    已解决
    0 回复