Linq使用标志列连接表

时间:2015-09-11 05:26:36

标签: c# winforms linq datatable .net-3.5

我需要使用名为cashbillingtype_id的列标志连接3个表,这将确定将加入哪个表。

例如,我将此数据表称为[CashBillings]:

cashbilling_id  cashbillingtype_id
1               1
2               1
3               2

当:

cashbillingtype_id = 1 means: CashBillingsBills
cashbillingtype_id = 2 means: CashBillingsReturns
cashbillingtype_id = 3 means: CashBillingsCancellations

现在每个表(BillsReturnsCancellations)都在一个名为cashbillingBRC_total的列中,我需要根据标志主数据表获取此列数据。

我试过:

(from CashBillings in _DataTable_Billings.AsEnumerable()
                                  join CashBillingsTypes in _DataTable_BillingsTypes.AsEnumerable()
                                      on CashBillings.Field<Int32>("cashbillingtype_id") equals CashBillingsTypes.Field<Int32>("cashbillingtype_id")
                                  select new
                 {
                     cashbilling_id = CashBillings.Field<Int32>("cashbilling_id"),
                     cashbillingBRC_total = (CashBillingsTypes.Field<Int32>("cashbillingtype_id") == 1 ?
                                                   (from CashBillingsBills in _DataTable_BillingsBills.AsEnumerable()
                                                    where CashBillingsBills.Field<Int32>("cashbilling_id") == CashBillings.Field<Int32>("cashbilling_id")
                                                    select CashBillingsBills.Field<Double>("cashbillingbill_total")).LastOrDefault()
                                                   :
                                                   (CashBillingsTypes.Field<Int32>("cashbillingtype_id") == 2 ?
                                                   (from CashBillingsReturns in _DataTable_BillingsReturns.AsEnumerable()
                                                    where CashBillingsReturns.Field<Int32>("cashbilling_id") == CashBillings.Field<Int32>("cashbilling_id")
                                                    where CashBillingsReturns.Field<Int32>("cashbillingreturnstatus_id") == 1 // Only Processed 
                                                    select CashBillingsReturns.Field<Double>("cashbillingreturn_total")).LastOrDefault()
                                                   :
                                                   (from CashBillingsCancellations in _DataTable_BillingsCancellations.AsEnumerable()
                                                    where CashBillingsCancellations.Field<Int32>("cashbilling_id") == CashBillings.Field<Int32>("cashbilling_id")
                                                    select CashBillingsCancellations.Field<Double>("cashbillingcancellation_total")).LastOrDefault())
                                                ),
                 }).Aggregate(DataTable_Billings, (dt, result) => { dt.Rows.Add(result.cashbilling_id, 
                                                                                result.cashbillingtype_id,
                                                                                result.cashbillingtype_name,
                                                                                result.cashbillingBRCstatus_id,
                                                                                result.cashbillingBRCstatus_name,
                                                                                result.cashbillingcustomer_fullname,
                                                                                result.cellar_name,
                                                                                result.cashbillingBRC_subtotal,
                                                                                result.cashbillingBRC_discount,
                                                                                result.cashbillingBRC_isv,
                                                                                result.cashbillingBRC_total,
                                                                                result.cashbillingBRC_date); return dt;
                 });

但是这段代码的效率非常低。

0 个答案:

没有答案