使用ViewModel的IQueryable Union不起作用

时间:2015-09-05 05:47:55

标签: c# entity-framework linq

我有两个返回相同IQueryable viewmodel的查询。

这是我的第一个问题:

var tempAfter = (from operation in _br_WarehouseOperation_GetAll.GetAll()
                 select new CardexSummaryViewModel
                         {
                             SumFirstImport = 0,
                             SumFirstExport = 0,
                             SumExport = g.Where(t => exportTypes == t.operation.TransactionTypeId).Sum(t => t.operationDetail.Quantity * t.operationDetail.UnitOfMeasure.ConversionFactor),
                             SumImport = g.Where(t => importTypes.Contains(t.operation.TransactionTypeId)).Sum(t => t.operationDetail.Quantity * t.operationDetail.UnitOfMeasure.ConversionFactor),
                             WarehouseGoodsId = warehouseGoods.WarehouseGoodsId,
                             SumPriceFirstImport = 0,
                             SumPriceFirstExport = 0,
                             SumPriceExport = g.Where(t => isPricing && exportTypes == t.operation.TransactionTypeId).Sum(t => t.operationDetail.WarehouseTransactionOperationPricings.Sum(it => it.TotalPrice)),
                             SumPriceImport = g.Where(t => isPricing && importTypes.Contains(t.operation.TransactionTypeId)).Sum(t => t.operationDetail.WarehouseTransactionOperationPricings.Sum(it => it.TotalPrice)),
                         });

这是第二个查询:

var tempBefore = (from operation in _br_WarehouseOperation_GetAll.GetAll()
                  select new CardexSummaryViewModel
                              {
                                  SumFirstImport = g.Where(t => importTypes.Contains(t.operation.TransactionTypeId)).Sum(t => t.operationDetail.Quantity * t.operationDetail.UnitOfMeasure.ConversionFactor),
                                  SumFirstExport = g.Where(t => exportTypes == t.operation.TransactionTypeId).Sum(t => t.operationDetail.Quantity * t.operationDetail.UnitOfMeasure.ConversionFactor),
                                  SumExport = 0,
                                  SumImport = 0,
                                  WarehouseGoodsId = warehouseGoods.WarehouseGoodsId,

                                  SumPriceFirstImport = g.Where(t => isPricing && importTypes.Contains(t.operation.TransactionTypeId))
                                            .Sum(t => t.operationDetail.WarehouseTransactionOperationPricings.Sum(it => it.TotalPrice)),
                                  SumPriceFirstExport = g.Where(t => isPricing && exportTypes == t.operation.TransactionTypeId)
                                            .Sum(t => t.operationDetail.WarehouseTransactionOperationPricings.Sum(it => it.TotalPrice)),
                                  SumPriceExport = 0,
                                  SumPriceImport = 0,
                              });

我想将这两个查询合并为一个这样的数据库调用:

 tempAfter = tempBefore.Union(tempAfter);
 cardexSummery = tempAfter.ToList();

但它不起作用并重复行。

我想知道是否有人可以告诉我在哪里错过了什么。感谢

1 个答案:

答案 0 :(得分:0)

两个linq语句都在创建新的CardexSummaryViewModel实例。由于嘿是不同的例子,联盟声明正在做它的工作。您需要在此类中提供所有相等性检查/覆盖,以确定实例是否实际相等。然后联盟将过滤掉重复的内容。仍然可以使其中一个linq语句中的两个实例相等,在这种情况下,Union不会过滤掉那些,只有来自两个不同语句的相等对象。因此,如果您希望删除所有重复项,您仍然希望在Union之后使用Distinct()。