using SqlSugar; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OrmTest { public class UnitSubToList002 { public static void Init() { var db = NewUnitTest.Db; var queryable = db.Queryable(); var abc = queryable .Select(j => new SolrJournalOut { Id = j.Id.SelectAll(), Languages = SqlFunc.Subqueryable().LeftJoin((l, jl) => l.Id == jl.LanguageId).Where((l, jl) => jl.JournalId == j.Id).ToList(l => l.Name), Subjects = SqlFunc.Subqueryable().LeftJoin((s, js) => s.Id == js.SubjectId).Where((s, js) => js.JournalId == j.Id).ToList(s => s.Name), }); var a = abc.ToSqlString(); if (a != "SELECT *,`j`.`Id` as app_ext_col_0 FROM `Journal` `j` ") { throw new Exception("unit error"); } } } /// /// /// public class Journal { /// /// 非自增ID /// [SugarColumn(ColumnDescription = "ID", IsPrimaryKey = true)] public long Id { get; set; } [Navigate(typeof(JournalLanguage), nameof(JournalLanguage.JournalId), nameof(JournalLanguage.LanguageId))] public List Languages { get; set; } [Navigate(NavigateType.OneToMany, nameof(JournalLanguage.JournalId))] public ICollection JournalLanguages { get; set; } [Navigate(typeof(JournalSubject), nameof(JournalSubject.JournalId), nameof(JournalSubject.SubjectId))] public List Subjects { get; set; } [Navigate(NavigateType.OneToMany, nameof(JournalSubject.JournalId))] public List JournalSubjects { get; set; } } /// /// /// public class JournalLanguage { /// /// 非自增ID /// [SugarColumn(ColumnDescription = "ID", IsPrimaryKey = true)] public long Id { get; set; } public long JournalId { get; set; } [Navigate(NavigateType.OneToOne, nameof(JournalId))]//一对一 public Journal Journal { get; set; } public long LanguageId { get; set; } [Navigate(NavigateType.OneToOne, nameof(Language))]//一对一 public Language Language { get; set; } } public class JournalSubject { /// /// 非自增ID /// [SugarColumn(ColumnDescription = "ID", IsPrimaryKey = true)] public long Id { get; set; } public long JournalId { get; set; } [Navigate(NavigateType.OneToOne, nameof(JournalId))]//一对一 public Journal Journal { get; set; } public long SubjectId { get; set; } [Navigate(NavigateType.OneToOne, nameof(SubjectId))]//一对一 public Subject Subject { get; set; } } public class Subject { /// /// 非自增ID /// [SugarColumn(ColumnDescription = "ID", IsPrimaryKey = true)] public long Id { get; set; } /// /// 名称 /// [MaxLength(500)] public string Name { get; set; } } public class Language { /// /// 非自增ID /// [SugarColumn(ColumnDescription = "ID", IsPrimaryKey = true)] public long Id { get; set; } /// /// 名称 /// [MaxLength(200)] public string Name { get; set; } } public class SolrJournalOut { public long Id { get; set; } public List Languages { get; set; } public List Subjects { get; set; } } }