分类:
ABP
GroupBy可根据单个字段、和对象筛选,它返回的是 IGrouping<out TKey, out TElement>
IGrouping的相关链接 https://www.itranslater.com/qa/details/2125804226692514816
如果要用多个字段筛选,可用GroupBy().GroupBy()....GroupBy(),每多一个GroupBy列表的深度就会多一层
注意巧妙运用:FirstOrDefaultAsync()、FirstOrDefault()、First(),主要用于去掉列表的深度(如:List的深度)
如1:
////货主的新老司机信息 //var CargoAndDrivers = await _CargoAndDriverRepository.GetAll().Where(_ => _.DriverId == OldDriverId || _.DriverId == NewDriverId).AsNoTracking().ToListAsync(); ////重叠的数据 var query = CargoAndDrivers.GroupBy(x => x.TenantId).Where(g => g.Count() > 1).ToList(); //货主新老司机重复的数据 var query2 = await _CargoAndDriverRepository.GetAll() .Where(_ => _.DriverId == OldDriverId || _.DriverId == NewDriverId) .GroupBy(x => x.TenantId) .Where(g => g.Count() > 1)//主要作用是有重复的对象就输出 .AsNoTracking().ToListAsync();
如2:
其实GroupBy之后的Select()里面的条件除了a.key是关于IGrouping的key外,其它的应该都是关于value的条件,即a对象的EF表达式
//查寻车辆的重叠的普通查询 var Vehers = _VehicleAndDriverRepository.GetAll() .Where(_ => TheSameOldCargoAndDriverIds.Contains(_.CargoAndDriverId) || TheSameNewCargoAndDriverIds.Contains(_.CargoAndDriverId)) .GroupBy(x => x.VehicleId) .Where(p => p.Count() > 1) /*其实Select里面的条件除了a.key是关于IGrouping的key外,其它的应该都是关于value的条件,即a对象 的EF表达式*/ .Select(a => a.Where(_ => TheSameNewCargoAndDriverIds.Contains(_.CargoAndDriverId))) .FirstOrDefault() .Select(a => a.Id).ToList();
如3:
//查寻车辆的重叠的异步查询 var CargoAndDriverIds = await _VehicleAndDriverRepository.GetAll().AsNoTracking() .Where(_ => TheSameOldCargoAndDriverIds.Contains(_.CargoAndDriverId) || TheSameNewCargoAndDriverIds.Contains(_.CargoAndDriverId)) .GroupBy(x => x.VehicleId) .Where(p => p.Count() > 1) .Select(a => a.Where(_ => TheSameNewCargoAndDriverIds.Contains(_.CargoAndDriverId)).Select(b => b.Id)) .FirstOrDefaultAsync();
案例:数据的复合,即查找重复的数据,并覆盖
/// <summary> /// 新、老司机的数据复合3 /// </summary> /// <param name="OldDriverUserId">旧的司机用户id</param> /// <param name="OldDriverId">旧的司机id</param> /// <param name="NewDriverId">新的司机id</param> /// <returns>重叠部分删除新的司机,原因:旧的数据只需要修改,重叠的数据只能存在一份,但是优先保留旧的数据</returns> public async Task DataComposition3(long OldDriverUserId, Guid OldDriverId, Guid NewDriverId) { try { ////其它数据、如运单 await _BusinessAppService.UpdateWayBillForDriver(NewDriverId, OldDriverId); //删除旧的实名司机 await _DriverRepository.DeleteAsync(_ => _.Id == OldDriverId); //删除司机的用户 await _userRepository.DeleteAsync(_ => _.Id == OldDriverUserId); //删除司机的用户、司机附件 await _FileInfoRepository.DeleteAsync(_ => _.BusinessId == OldDriverId.ToString() || _.BusinessId == OldDriverUserId.ToString()); using (_IUnitOfWorkRepository.Current.DisableFilter(AbpDataFilters.MayHaveTenant, AbpDataFilters.MustHaveTenant))//禁用MayHaveTenant过滤 { //货主新老司机重复的数据 var query2 = await _CargoAndDriverRepository.GetAll() .Where(_ => _.DriverId == OldDriverId || _.DriverId == NewDriverId) .GroupBy(x => x.TenantId) .Where(g => g.Count() > 1) .AsNoTracking().ToListAsync(); if (query2.Count() == 0) { return; } //重叠的新、旧货主司机 var TheSameOldCargoAndDrivers = query2.Select(a => a.Where(_ => _.DriverId == OldDriverId)).FirstOrDefault(); if (TheSameOldCargoAndDrivers.Count() == 0) { return; } var TheSameNewCargoAndDrivers = query2.Select(a => a.Where(_ => _.DriverId == NewDriverId)).FirstOrDefault(); //id var TheSameOldCargoAndDriverIds = TheSameOldCargoAndDrivers.Select(_ => _.Id); var TheSameNewCargoAndDriverIds = TheSameNewCargoAndDrivers.Select(_ => _.Id); //需要删除的新司机 await _CargoAndDriverRepository.DeleteAsync(_ => TheSameNewCargoAndDriverIds.Contains(_.Id)); //查寻车辆的重叠的异步查询 var CargoAndDriverIds = await _VehicleAndDriverRepository.GetAll().AsNoTracking() .Where(_ => TheSameOldCargoAndDriverIds.Contains(_.CargoAndDriverId) || TheSameNewCargoAndDriverIds.Contains(_.CargoAndDriverId)) .GroupBy(x => x.VehicleId) .Where(p => p.Count() > 1) .Select(a => a.Where(_ => TheSameNewCargoAndDriverIds.Contains(_.CargoAndDriverId)).Select(b => b.Id)) .FirstOrDefaultAsync(); //得到需要删除的车辆与司机关系的id await _VehicleAndDriverRepository.DeleteAsync(_ => CargoAndDriverIds.Contains(_.Id)); //把以前司机的车辆修改为现在司机的车辆 await _VehicleRepository.BatchUpdateAsync(p => new Vehicle { DriverId = NewDriverId }, p => p.DriverId == OldDriverId); //把以前司机的车辆修改为现在司机的车辆 await _CargoAndDriverRepository.BatchUpdateAsync(p => new CargoAndDriver { DriverId = NewDriverId }, p => p.DriverId == OldDriverId); return; } } catch (Exception ex) { throw new UserFriendlyException(ex.Message); } }
评价
排名
6
文章
6
粉丝
16
评论
8
{{item.articleTitle}}
{{item.blogName}} : {{item.content}}
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2024TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术