两个List数据集合合并成一个List

开发工具与关键技术:Visual Studio 与C#

作者:黄灿

撰写时间:2019.7.25

查询两个差不多相同的表,一个是入库表,一个是退货表,字段大致相同,但是有所差异,入库表的数量和所有金额都是正数,退货表的数量和金额都是负数,所以不能join链表查询,需要分开查询出两个ToList;但是return只能有一个,不能return两次,并且查询出来的数据是放到一个结算表,也不能有两个action。所以我查询两次,一次join入库表,一次join退货表,查询出来的数据到两个List里



List<SupplierVo>
listStorageSettlement = (from tbSupplier in myModels.SYS_Supplier
			join tbPurchaseStorage in myModels.PW_PurchaseStorage on tbSupplier.SupplierID equals tbPurchaseStorage.SupplierID
			join tbInvoicesType in myModels.SYS_InvoicesType on tbPurchaseStorage.InvoicesTypeID equals
			tbInvoicesType.InvoicesTypeID
			where tbSupplier.SupplierID == SupplierID&&
			tbPurchaseStorage.SettleStateID!=2&&tbPurchaseStorage.SettleStateID!=4
			select new SupplierVo
			{
				InvoicesType =tbInvoicesType.InvoicesType,//单据类型
				InvoicesTypeID =tbInvoicesType.InvoicesTypeID,//单据类型
				StorageNumber =tbPurchaseStorage.StorageNumber,//入库单号
				PurchaseStorageID =tbPurchaseStorage.PurchaseStorageID,//入库ID
				makeFormDate = tbPurchaseStorage.MakeFormDate.ToString(),//入库单据日期
				amountMoney =tbPurchaseStorage.AmountMoney,//入库应付金额
				PaidAmount = 0,//已付金额
				OutstandingAmount =tbPurchaseStorage.AmountMoney,//未付金额
				DiscountedTotalAmount= 0,//已优惠金额
				ThisDiscountAmount =0,//本次优惠金额
				ThisPaymentAmount =tbPurchaseStorage.AmountMoney,//本次付款金额
			}).ToList();
List<SupplierVo>
listReturnSettlement = (from tbSupplier in myModels.SYS_Supplier
			join tbPurchaseReturn in myModels.PW_PurchaseReturn on tbSupplier.SupplierID equals tbPurchaseReturn.SupplierID
			join tbInvoicesType in myModels.SYS_InvoicesType on tbPurchaseReturn.InvoicesTypeID equals tbInvoicesType.InvoicesTypeID
			where tbPurchaseReturn.SupplierID == SupplierID &&tbPurchaseReturn.SettleStateID != 2 &&tbPurchaseReturn.SettleStateID!= 4
			select new SupplierVo
			{
		
				InvoicesType =tbInvoicesType.InvoicesType,///单据类型
				InvoicesTypeID =tbInvoicesType.InvoicesTypeID,///单据类型
				ReturnNumber =tbPurchaseReturn.ReturnNumber,//退货单号
				PurchaseReturnID = tbPurchaseReturn.PurchaseReturnID,//退货ID
				makeFormDate =tbPurchaseReturn.MakeFormDate.ToString(),//退货单据日期
				amountMoney =tbPurchaseReturn.AmountMoney,//退货应付金额
				PaidAmount = 0,//已付金额
				OutstandingAmount =tbPurchaseReturn.AmountMoney,//未付金额
				DiscountedTotalAmount= 0,//已优惠金额
				ThisDiscountAmount =0,//本次优惠金额
				ThisPaymentAmount =tbPurchaseReturn.AmountMoney,//本次付款金额                                                 
			}).ToList();

查询的两个List集合List<实体类>必须是同一个不然无法合并,查询出两个List数据集合后,使用



IEnumerable<SupplierVo> intsResult =listStorageSettlement.Union(listReturnSettlement)


进行重新实例一个集合,IEnumerable 公开枚举,集合可以进行简单的迭代传送,Union是拼接两个List数据集合的并集,最后只要返回intsResult就可以把两个ToList的数据都return

猜你喜欢

转载自blog.csdn.net/weixin_44542088/article/details/97277833