我在LINQ联接中有一个非常奇怪的行为
我正在加入一个IEnumerable集合和一个我通过Entity Framework访问的视图。
例如(数字是列值):
集合
1 1
1 2
查看
1 3
1 4
联接的结果应该是
1 1 1 3
1 1 1 4
1 2 1 3
1 2 1 4
我在管理工作室得到了T-SQL的结果,甚至在LinqPad中使用了相同的代码, 但是在VS中我得到了
1 1 1 3
1 1 1 3
1 2 1 3
1 2 1 3
或
1 1 1 4
1 1 1 4
1 2 1 4
1 2 1 4
但从未正确加入。我通过SQL Server Profiler检查Linq生成的查询并在Managment Stuiod中启动它,并得到了正确的答案。请帮助我理解这种非常奇怪的行为
var allocations = InvoicingContext.vAllocations.Where(x => auditIds.Contains(x.AuditGroupId.Value)).AsEnumerable();
var q = from a in allocations
join c in CommonCodesContext.vContainerEntity on a.ContainerId equals c.ContainerId
select new ContainerServiceModel
{
ContainerNo = a.ContainerNo,
PortATD = c.ATD,
ATA_KL = a.ServiceDate,
RateWithoutVat = a.RateWithoutVat,
DestinationCityId = c.DestinationCityId,
Quantity = 1
};
var containerServiceModels = q.ToList();
int destinationCount = containerServiceModels.Where(x => x.DestinationCityId == 14).Count();
为Linq代码生成的查询(Visual Studio)
SELECT
[Extent1].[ShipperID] AS [ShipperID],
[Extent1].[CarrierId] AS [CarrierId],
[Extent1].[ShippingLineID] AS [ShippingLineID],
[Extent1].[TerminalId] AS [TerminalId],
[Extent1].[Terminal] AS [Terminal],
[Extent1].[ContainerId] AS [ContainerId],
[Extent1].[ContainerNo] AS [ContainerNo],
[Extent1].[ContTypeID] AS [ContTypeID],
[Extent1].[PortStorageDays] AS [PortStorageDays],
[Extent1].[UnloadDate] AS [UnloadDate],
[Extent1].[ATA] AS [ATA],
[Extent1].[ATD] AS [ATD],
[Extent1].[PortShipped] AS [PortShipped],
[Extent1].[IsSealed] AS [IsSealed],
[Extent1].[ATA_CY] AS [ATA_CY],
[Extent1].[ATD_CY] AS [ATD_CY],
[Extent1].[InspectionDate] AS [InspectionDate],
[Extent1].[OrderId] AS [OrderId],
[Extent1].[OrderTypeId] AS [OrderTypeId],
[Extent1].[StorageTypeId] AS [StorageTypeId],
[Extent1].[LoadRegionId] AS [LoadRegionId],
[Extent1].[LoadCityId] AS [LoadCityId],
[Extent1].[DestinationRegionId] AS [DestinationRegionId],
[Extent1].[DestinationCityId] AS [DestinationCityId],
[Extent1].[ODCYCustomer] AS [ODCYCustomer],
[Extent1].[ODCYShipped] AS [ODCYShipped],
[Extent1].[PortReturnDate] AS [PortReturnDate],
[Extent1].[IsReturnedToPort] AS [IsReturnedToPort],
[Extent1].[ODCYStorageDays] AS [ODCYStorageDays],
[Extent1].[DetentionDays] AS [DetentionDays],
[Extent1].[CCFinish] AS [CCFinish],
[Extent1].[FreeDaysForTrucker] AS [FreeDaysForTrucker],
[Extent1].[TruckingUnloadDays] AS [TruckingUnloadDays],
[Extent1].[TruckingEmptyReturnDays] AS [TruckingEmptyReturnDays],
[Extent1].[TruckingEmptyReturnDetentionDays] AS [TruckingEmptyReturnDetentionDays],
[Extent1].[LatePickUpDays] AS [LatePickUpDays]
FROM (SELECT
[vContainerEntity].[ShipperID] AS [ShipperID],
[vContainerEntity].[CarrierId] AS [CarrierId],
[vContainerEntity].[ShippingLineID] AS [ShippingLineID],
[vContainerEntity].[TerminalId] AS [TerminalId],
[vContainerEntity].[Terminal] AS [Terminal],
[vContainerEntity].[ContainerId] AS [ContainerId],
[vContainerEntity].[ContainerNo] AS [ContainerNo],
[vContainerEntity].[ContTypeID] AS [ContTypeID],
[vContainerEntity].[PortStorageDays] AS [PortStorageDays],
[vContainerEntity].[UnloadDate] AS [UnloadDate],
[vContainerEntity].[ATA] AS [ATA],
[vContainerEntity].[ATD] AS [ATD],
[vContainerEntity].[PortShipped] AS [PortShipped],
[vContainerEntity].[IsSealed] AS [IsSealed],
[vContainerEntity].[ATA_CY] AS [ATA_CY],
[vContainerEntity].[ATD_CY] AS [ATD_CY],
[vContainerEntity].[InspectionDate] AS [InspectionDate],
[vContainerEntity].[OrderId] AS [OrderId],
[vContainerEntity].[OrderTypeId] AS [OrderTypeId],
[vContainerEntity].[StorageTypeId] AS [StorageTypeId],
[vContainerEntity].[LoadRegionId] AS [LoadRegionId],
[vContainerEntity].[LoadCityId] AS [LoadCityId],
[vContainerEntity].[DestinationRegionId] AS [DestinationRegionId],
[vContainerEntity].[DestinationCityId] AS [DestinationCityId],
[vContainerEntity].[ODCYCustomer] AS [ODCYCustomer],
[vContainerEntity].[ODCYShipped] AS [ODCYShipped],
[vContainerEntity].[PortReturnDate] AS [PortReturnDate],
[vContainerEntity].[IsReturnedToPort] AS [IsReturnedToPort],
[vContainerEntity].[ODCYStorageDays] AS [ODCYStorageDays],
[vContainerEntity].[DetentionDays] AS [DetentionDays],
[vContainerEntity].[CCFinish] AS [CCFinish],
[vContainerEntity].[FreeDaysForTrucker] AS [FreeDaysForTrucker],
[vContainerEntity].[TruckingUnloadDays] AS [TruckingUnloadDays],
[vContainerEntity].[TruckingEmptyReturnDays] AS [TruckingEmptyReturnDays],
[vContainerEntity].[TruckingEmptyReturnDetentionDays] AS [TruckingEmptyReturnDetentionDays],
[vContainerEntity].[LatePickUpDays] AS [LatePickUpDays]
FROM [Tariff].[vContainerEntity] AS [vContainerEntity]) AS [Extent1]
和
SELECT
[Extent1].[id] AS [id],
[Extent1].[Payer] AS [Payer],
[Extent1].[Receiver] AS [Receiver],
[Extent1].[CustomerId] AS [CustomerId],
[Extent1].[CustomerName] AS [CustomerName],
[Extent1].[AuditGroupId] AS [AuditGroupId],
[Extent1].[ContainerId] AS [ContainerId],
[Extent1].[OrderId] AS [OrderId],
[Extent1].[ContainerNo] AS [ContainerNo],
[Extent1].[TariffId] AS [TariffId],
[Extent1].[ServiceTypeId] AS [ServiceTypeId],
[Extent1].[ServiceType] AS [ServiceType],
[Extent1].[ServiceDate] AS [ServiceDate],
[Extent1].[IsServiceDateClosed] AS [IsServiceDateClosed],
[Extent1].[InvoiceId] AS [InvoiceId],
[Extent1].[InvoiceNo] AS [InvoiceNo],
[Extent1].[InvoiceDate] AS [InvoiceDate],
[Extent1].[ReceiveDate] AS [ReceiveDate],
[Extent1].[DueDate] AS [DueDate],
[Extent1].[ResponsibleUser] AS [ResponsibleUser],
[Extent1].[Free] AS [Free],
[Extent1].[PaymentQty] AS [PaymentQty],
[Extent1].[RateWithoutVat] AS [RateWithoutVat],
[Extent1].[RateWithVat] AS [RateWithVat],
[Extent1].[AmountWithoutVat] AS [AmountWithoutVat],
[Extent1].[CommissionWithoutVat] AS [CommissionWithoutVat],
[Extent1].[TotalAmountWithoutVat] AS [TotalAmountWithoutVat],
[Extent1].[AmountWithVat] AS [AmountWithVat],
[Extent1].[CommissionWithVat] AS [CommissionWithVat],
[Extent1].[TotalAmountWithVat] AS [TotalAmountWithVat],
[Extent1].[VatSum] AS [VatSum],
[Extent1].[RateWithoutVatRub] AS [RateWithoutVatRub],
[Extent1].[RateWithVatRub] AS [RateWithVatRub],
[Extent1].[AmountWithoutVatRub] AS [AmountWithoutVatRub],
[Extent1].[CommissionAmountWithoutVatInRub] AS [CommissionAmountWithoutVatInRub],
[Extent1].[TotalAmountWithoutVatRub] AS [TotalAmountWithoutVatRub],
[Extent1].[AmountWithVatRub] AS [AmountWithVatRub],
[Extent1].[CommissionAmountWithVatInRub] AS [CommissionAmountWithVatInRub],
[Extent1].[TotalAmountWithVatRub] AS [TotalAmountWithVatRub],
[Extent1].[AmountVatRub] AS [AmountVatRub],
[Extent1].[CommissionVatRub] AS [CommissionVatRub],
[Extent1].[VatSumInRub] AS [VatSumInRub],
[Extent1].[CurrencyId] AS [CurrencyId],
[Extent1].[IncludeVat] AS [IncludeVat],
[Extent1].[VAT] AS [VAT],
[Extent1].[IncludeVatInCommission] AS [IncludeVatInCommission],
[Extent1].[ExchangeDate] AS [ExchangeDate],
[Extent1].[ExchangeDateForCommission] AS [ExchangeDateForCommission],
[Extent1].[ExchangeRate] AS [ExchangeRate],
[Extent1].[CommissionExchangeRate] AS [CommissionExchangeRate],
[Extent1].[AverageExchangeRate] AS [AverageExchangeRate],
[Extent1].[StatusId] AS [StatusId],
[Extent1].[Remark] AS [Remark],
[Extent1].[Author] AS [Author],
[Extent1].[DtCreated] AS [DtCreated]
FROM (SELECT
[vAllocations].[id] AS [id],
[vAllocations].[Payer] AS [Payer],
[vAllocations].[Receiver] AS [Receiver],
[vAllocations].[CustomerId] AS [CustomerId],
[vAllocations].[CustomerName] AS [CustomerName],
[vAllocations].[AuditGroupId] AS [AuditGroupId],
[vAllocations].[ContainerId] AS [ContainerId],
[vAllocations].[OrderId] AS [OrderId],
[vAllocations].[ContainerNo] AS [ContainerNo],
[vAllocations].[TariffId] AS [TariffId],
[vAllocations].[ServiceTypeId] AS [ServiceTypeId],
[vAllocations].[ServiceType] AS [ServiceType],
[vAllocations].[ServiceDate] AS [ServiceDate],
[vAllocations].[IsServiceDateClosed] AS [IsServiceDateClosed],
[vAllocations].[InvoiceId] AS [InvoiceId],
[vAllocations].[InvoiceNo] AS [InvoiceNo],
[vAllocations].[InvoiceDate] AS [InvoiceDate],
[vAllocations].[ReceiveDate] AS [ReceiveDate],
[vAllocations].[DueDate] AS [DueDate],
[vAllocations].[ResponsibleUser] AS [ResponsibleUser],
[vAllocations].[Free] AS [Free],
[vAllocations].[PaymentQty] AS [PaymentQty],
[vAllocations].[RateWithoutVat] AS [RateWithoutVat],
[vAllocations].[RateWithVat] AS [RateWithVat],
[vAllocations].[AmountWithoutVat] AS [AmountWithoutVat],
[vAllocations].[CommissionWithoutVat] AS [CommissionWithoutVat],
[vAllocations].[TotalAmountWithoutVat] AS [TotalAmountWithoutVat],
[vAllocations].[AmountWithVat] AS [AmountWithVat],
[vAllocations].[CommissionWithVat] AS [CommissionWithVat],
[vAllocations].[TotalAmountWithVat] AS [TotalAmountWithVat],
[vAllocations].[VatSum] AS [VatSum],
[vAllocations].[RateWithoutVatRub] AS [RateWithoutVatRub],
[vAllocations].[RateWithVatRub] AS [RateWithVatRub],
[vAllocations].[AmountWithoutVatRub] AS [AmountWithoutVatRub],
[vAllocations].[CommissionAmountWithoutVatInRub] AS [CommissionAmountWithoutVatInRub],
[vAllocations].[TotalAmountWithoutVatRub] AS [TotalAmountWithoutVatRub],
[vAllocations].[AmountWithVatRub] AS [AmountWithVatRub],
[vAllocations].[CommissionAmountWithVatInRub] AS [CommissionAmountWithVatInRub],
[vAllocations].[TotalAmountWithVatRub] AS [TotalAmountWithVatRub],
[vAllocations].[AmountVatRub] AS [AmountVatRub],
[vAllocations].[CommissionVatRub] AS [CommissionVatRub],
[vAllocations].[VatSumInRub] AS [VatSumInRub],
[vAllocations].[CurrencyId] AS [CurrencyId],
[vAllocations].[IncludeVat] AS [IncludeVat],
[vAllocations].[VAT] AS [VAT],
[vAllocations].[IncludeVatInCommission] AS [IncludeVatInCommission],
[vAllocations].[ExchangeDate] AS [ExchangeDate],
[vAllocations].[ExchangeDateForCommission] AS [ExchangeDateForCommission],
[vAllocations].[ExchangeRate] AS [ExchangeRate],
[vAllocations].[CommissionExchangeRate] AS [CommissionExchangeRate],
[vAllocations].[AverageExchangeRate] AS [AverageExchangeRate],
[vAllocations].[StatusId] AS [StatusId],
[vAllocations].[Remark] AS [Remark],
[vAllocations].[Author] AS [Author],
[vAllocations].[DtCreated] AS [DtCreated]
FROM [dbo].[vAllocations] AS [vAllocations]) AS [Extent1]
WHERE ([Extent1].[AuditGroupId] IN (9107)) AND ([Extent1].[AuditGroupId] IS NOT NULL)
在LinqPAD测试中生成的查询
exec sp_executesql N'SELECT [t0].[ContainerNo], [t1].[ATD] AS [PortATD], [t0].[ServiceDate] AS [ATA_KL], [t0].[RateWithoutVat], [t1].[DestinationCityId]
FROM [Invoicing_SPB].[dbo].[vAllocations] AS [t0]
INNER JOIN [Tariff].[vContainerEntity] AS [t1] ON ([t0].[ContainerId]) = [t1].[ContainerId]
WHERE ([t1].[DestinationCityId] = @p0) AND ([t0].[AuditGroupId] = @p1)',N'@p0 int,@p1 int',@p0=14,@p1=9107
我试图循环并根据谓词来计算结果集合
Invoicing_SPBEntities invContext = new Invoicing_SPBEntities();
CommonCodesEntities ccContext = new CommonCodesEntities();
for (int i = 0; i <= 20; i++)
{
var allocations = (from a in invContext.vAllocations
where a.AuditGroupId == 9107
select a).AsEnumerable();
var q = from a in allocations
join c in ccContext.vContainerEntity on a.ContainerId equals c.ContainerId
select new
{
ContainerNo = a.ContainerNo,
PortATD = c.ATD,
ATA_KL = a.ServiceDate,
RateWithoutVat = a.RateWithoutVat,
OrderTypeId = c.OrderTypeId,
DestinationCityId = c.DestinationCityId,
Quantity = 1
};
var result = q.ToList();
Console.WriteLine(result.Where(x=>x.DestinationCityId == 14).ToList().Count);
}
产生的控制台输出
64 62 58 58 64 62 60 60 56 60 62 60 58 54 56 64 58
非常奇怪:D