Select 匿名对象映射问题 返回
SqlSugar
处理完成
2
281
老菜鸟了 发布于1个月前
悬赏:0 飞吻
使用导航查询时异常 var list = await sugarScope.Queryable<Goods>().Includes(i => i.Category).Where(where).Select<object>(i => new { all = i.Id.SelectAll(), categoryname = i.Category.Name }).ToListAsync(); //报异常: System.InvalidOperationException: Sequence contains no elements at System.Linq.ThrowHelper.ThrowNoElementsException() at System.Linq.Enumerable.First[TSource](IEnumerable`1 source) at SqlSugar.QueryableProvider`1.<SelectNavQuery>b__210_0[TResult](EntityColumnInfo a) at System.Linq.Enumerable.WhereListIterator`1.ToList() at SqlSugar.QueryableProvider`1.SelectNavQuery[TResult](List`1 result, List`1 managers) at SqlSugar.QueryableProvider`1._InitNavigat[TResult](List`1 result) at SqlSugar.QueryableProvider`1.<>c__DisplayClass208_0`1.<_InitNavigatAsync>b__0() at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) --- End of stack trace from previous location --- at System.Threading.ExecutionContext.RunFromThreadPoolDispatchLoop(Thread threadPoolThread, ExecutionContext executionContext, ContextCallback callback, Object state) at System.Threading.Tasks.Task.ExecuteWithThreadLocal(Task& currentTaskSlot, Thread threadPoolThread) --- End of stack trace from previous location --- at SqlSugar.QueryableProvider`1._InitNavigatAsync[TResult](List`1 result) at SqlSugar.QueryableProvider`1._ToListAsync[TResult]() //生成sql sql:SELECT *, (SELECT `Name` FROM `GoodsCategory` WHERE `i`.`CategoryId`=`Id` ) AS `categoryname` ,`i`.`Id` AS SugarNav_Id,`i`.`CategoryId` AS SugarNav_CategoryId FROM `Goods` `i` WHERE ( 1 = 1 ) AND ( `IsDeleted` = @IsDeleted0 ) params: @IsDeleted0:0
//使用连表查询时正常 var list = await sugarScope.Queryable<Goods>().LeftJoin<GoodsCategory>((g, c) => g.CategoryId == c.Id).Where(where).Select<object>((g, c) => new { Id = g.Id.SelectAll(), categoryname = c.Name }).ToListAsync(); //生成sql sql:SELECT `g`.*, `c`.`Name` AS `categoryname` FROM `Goods` `g` Left JOIN `GoodsCategory` `c` ON ( `g`.`CategoryId` = `c`.`Id` ) AND ( `c`.`IsDeleted` = @IsDeleted0 ) WHERE ( 1 = 1 ) AND ( `g`.`IsDeleted` = @IsDeleted0 ) params: @IsDeleted0:0
[SugarIndex("unique_goods_code", nameof(Code), OrderByType.Asc, true)] public class Goods : IDeleted, ITable { /// <summary> /// 业务外主键 /// </summary> [SugarColumn(IsPrimaryKey = true)] public long Id { get; set; } /// <summary> /// 物料编码 /// </summary> [SugarColumn(Length = 22)] public string Code { get; set; } = null!; /// <summary> /// 物料条码 /// </summary> public string? Barcode { get; set; } /// <summary> /// 物料名称 /// </summary> [SugarColumn(Length = 50)] public string Name { get; set; } = null!; /// <summary> /// 物料类别id /// </summary> public long CategoryId { get; set; } /// <summary> /// 逻辑删除标记 /// </summary> public int IsDeleted { get; set; } [Navigate(NavigateType.OneToOne, nameof(CategoryId))] public GoodsCategory? Category { get; set; } } [SugarIndex("unique_category_code", nameof(Code), OrderByType.Asc, true)] public class GoodsCategory : IDeleted, ITable { /// <summary> /// 业务外主键 /// </summary> [SugarColumn(IsPrimaryKey = true)] public long Id { get; set; } /// <summary> /// 编码 /// </summary> [SugarColumn(Length = 6)] public string Code { get; set; } = null!; /// <summary> /// 上级分类id /// </summary> public long ParentId { get; set; } /// <summary> /// 名称 /// </summary> [SugarColumn(Length = 20)] public string Name { get; set; } = null!; /// <summary> /// 逻辑删除标记 /// </summary> public int IsDeleted { get; set; } = 0; [Navigate(NavigateType.OneToOne, nameof(ParentId))] public GoodsCategory? Parent { get; set; } [SugarColumn(IsIgnore = true)] public List<GoodsCategory>? Children { get; set; } }
热忱回答(2)
-
老菜鸟了 VIP01个月前
是我自己搞错了 使用select时 不需要 include
0 回复 -
fate sta VIP01个月前
all = i.Id.SelectAll(), 这个不能用,可以select,看文档:导航 里面的DTO写法
0 回复