简体   繁体   English

与ViewModel的IQueryable Union不起作用

[英]IQueryable Union with ViewModel does not work

I have two queries that return the same IQueryable viewmodel. 我有两个查询,它们返回相同的IQueryable视图模型。

This is my first query: 这是我的第一个查询:

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)),
                         });

and this is second query : 这是第二个查询:

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,
                              });

I want to union these two queries into one database call like this : 我想将这两个查询合并为一个数据库调用,如下所示:

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

but it does not work and duplicates the rows. 但它不起作用,并且复制了行。

I was wondering if anybody could tell me where I am missing something. 我想知道是否有人可以告诉我我缺少什么。 Thanks 谢谢

Both linq statements are creating new CardexSummaryViewModel instances. 这两个linq语句都在创建新的CardexSummaryViewModel实例。 Since hey are distinct instances the Union statement is doing it's job. 由于嘿是不同的实例,所以工会声明正在履行职责。 You would need to provide all the equality checks/overrides in this class to determine if the instances are actually equal. 您将需要在此类中提供所有相等性检查/替代,以确定实例实际上是否相等。 Then the Union would filter out duplicates. 然后,联盟将过滤掉重复项。 It would still be possible to have two instances from one of the linq statements be equal, in which case, the Union would not filter those out, only equal objects from the two different statements. 仍然有可能使linq语句之一的两个实例相等,在这种情况下,Union不会从两个不同的语句中仅将相等的对象过滤掉。 So, you would still want to use Distinct() after Union if you want all duplicates removed. 因此,如果要删除所有重复项,仍然希望在Union之后使用Distinct()。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM