老导航查询文档备份 返回
SqlSugar
老数据
1
4856
fate sta 发布于2022/4/14
悬赏:5 飞吻
1、一对一查询
使用Mapper一对一模式需要设置2-3个参数, Mapper( 1需要填充的子对象 ,2主表关联字段,3从表关联字段(默认为从表主键))
第一个参数:it.Order是填充的子对象
第二个参数 it.OrderId是主表中关联字段
第三个参数 不填等于从表主键,也可以指定从表某个字段
其实就是 主表中的 it.OrderId 和从表中的主键进行关联 ,就能自动填充到 OrderItem.Order里面
//主表(OrderItem )中OrderId与从表(Order)中的主键关联 var list= db.Queryable<OrderItem>().Mapper(it => it.Order, it => it.OrderId).ToList(); foreach(var item in list) { Console.WriteLine(item.Order.Name);//输出子对象中的值 } public class OrderItem { [SugarColumn(IsPrimaryKey =true ,IsIdentity =true)] public int ItemId { get; set; } public string ItemCode { get; set; } public decimal Pirce { get; set; } public int OrderId { get; set; } [SugarColumn(IsIgnore =true)] public Order Order{ get; set; } } public class Order { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int Id { get; set; } public string Name { get; set; } }
下面语句等于同上面语句,当没有主键的时候可以手动指定从表字段
var list= db.Queryable<OrderItem>().Mapper(it => it.Order, it => it.OrderId , it=>it.Order.Id ).ToList();
新功能:1对1模式中支持了条件过滤
var list= db.Queryable<OrderItem>() .Mapper(it => it.Order, it => it.OrderId) //我们可以使用2级对象属性进行筛选,要写在Mapper后面 .Where(it=>it.Order.Name=="哈哈").ToList(); //where中的it要与mapper中的一样
2、一对多查询
根据子对象items中的OrderId与主表的主键进行关联
public class Order { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int Id { get; set; } public string Name { get; set; } [SugarColumn(IsIgnore = true)] public List<OrderItem> Items { get; set; } } public class OrderItem { [SugarColumn(IsPrimaryKey =true ,IsIdentity =true)] public int ItemId { get; set; } public string ItemCode { get; set; } public decimal Pirce { get; set; } public int OrderId { get; set; } } var list= db.Queryable<Order>().Mapper(it => it.Items, it => it.Items.First().OrderId).ToList(); //主表进行过滤,根据子对象 var list= db.Queryable<Order>() .Mapper(it => it.Items, it => it.Items.First().OrderId) .Where(it=>it.Items.Count()>0)//或者it.Items.Any() //where中的it要与mapper中的一样 .ToList(); //只针对子集合过滤 var list= db.Queryable<Order>() .Mapper(it => it.Items, it => it.Items.First().OrderId) .Mapper(it=>{ //等号不能少,除了过滤也可以排序 it.Items=it.Items.Where(it=>it.id>10).ToList(); }) .ToList();
注:5.0.2.4版本支持了 it.Items.Any(y=>y.itemid==1) 或者Count(y=>y.itemid==1)
3、 多对多查询
只需按顺序入队,实体名称随意取
var list9= Db.Queryable<TableA>() .Mapper<TableA, TableB, ABMap>(it => ManyToMany.Config(it.Aid, it.Bid)).ToList(); //list9[0].ChildList 就会有值 //如果是这样的顺序 <TableA, TableB, ABMap> 那么 ManyToMany.Config(it.Aid, it.Bid) ID的顺序也一样就行
实体结构非常清晰
public class TableA{ [SugarColumn(IsPrimaryKey =true,IsIdentity =true)] public int id { get; set; } public string Name { get; set; } [SugarColumn(IsIgnore =true)] public List<TableB> Childlist { get; set; } } //中间表 public class ABMap{ public int Aid { get; set; } public int Bid { get; set; } } public class TableB { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int id { get; set; } public string Name { get; set; } }
4、相同字段名映射
子表对象=子表对象.where(it=>it.OrgId==主表对象中的.OrgId)
public class A { [SugarColumn(IsPrimaryKey =true ,IsIdentity =true)] public int Id { get; set; } public string Name { get; set; } public string OrgId { get; set; } [SugarColumn(IsIgnore =true)] public B B { get; set; } } public class B { [SugarColumn(IsPrimaryKey = true, IsIdentity = true)] public int Id { get; set; } public string Name { get; set; } public string OrgId { get; set; } } var list= db.Queryable<A>().Mapper(it => it.B,it=>it.OrgId).ToList();
热忱回答(1)
-
极客飞鹏 VIP02022/5/160 回复