Sounds obscure but somehow I hit it.
My examples are from LinqPad against Northwind.
Filter methods, the top two are logically the same:
public static IQueryable<EmployeeEntity> FilterByCustomerTypeIdViaOrders(this IQueryable<EmployeeEntity> employees, string customerTypeId)
{
return from e in employees
from c in e.CustomersViaOrders
from ccd in c.CustomerCustomerDemos
where ccd.CustomerTypeId == customerTypeId
select e;
}
public static IQueryable<EmployeeEntity> FilterByCustomerTypeId(this IQueryable<EmployeeEntity> employees, string customerTypeId)
{
return from e in employees
from o in e.Orders
from ccd in o.Customer.CustomerCustomerDemos
where ccd.CustomerTypeId == customerTypeId
select e;
}
public static IQueryable<EmployeeEntity> FilterByOrder(this IQueryable<EmployeeEntity> employees, int orderId)
{
return from e in employees
from o in e.Orders
where o.OrderId == orderId
select e;
}
This
Employee.FilterByOrder(1).FilterByCustomerTypeId("ALFKI")
gives
DECLARE @p1 Int; SET @p1=1
DECLARE @p2 NChar(10); SET @p2='ALFKI'
SELECT [LPA_L1].[Address],
[LPA_L1].[BirthDate],
[LPA_L1].[City],
[LPA_L1].[Country],
[LPA_L1].[EmployeeId],
[LPA_L1].[Extension],
[LPA_L1].[FirstName],
[LPA_L1].[HireDate],
[LPA_L1].[HomePhone],
[LPA_L1].[LastName],
[LPA_L1].[Notes],
[LPA_L1].[Photo],
[LPA_L1].[PhotoPath],
[LPA_L1].[PostalCode],
[LPA_L1].[Region],
[LPA_L1].[ReportsTo],
[LPA_L1].[Title],
[LPA_L1].[TitleOfCourtesy]
FROM
((( (SELECT [LPA_L5].[Address],
[LPA_L5].[BirthDate],
[LPA_L5].[City],
[LPA_L5].[Country],
[LPA_L5].[EmployeeID] AS [EmployeeId],
[LPA_L5].[Extension],
[LPA_L5].[FirstName],
[LPA_L5].[HireDate],
[LPA_L5].[HomePhone],
[LPA_L5].[LastName],
[LPA_L5].[Notes],
[LPA_L5].[Photo],
[LPA_L5].[PhotoPath],
[LPA_L5].[PostalCode],
[LPA_L5].[Region],
[LPA_L5].[ReportsTo],
[LPA_L5].[Title],
[LPA_L5].[TitleOfCourtesy]
FROM
( [dbo].[Employees] [LPA_L5]
INNER JOIN
[dbo].[Orders] [LPA_L6] ON [LPA_L5].[EmployeeID]=[LPA_L6].[EmployeeID])
WHERE
( ( ( [LPA_L6].[OrderID] = @p1)))) [LPA_L1]
INNER JOIN
[dbo].[Orders] [LPA_L2] ON ( [LPA_L1].[EmployeeId] = [LPA_L2].[EmployeeID]))
LEFT JOIN
[dbo].[Customers] [LPA_L3] ON [LPA_L3].[CustomerID]=[LPA_L2].[CustomerID])
INNER JOIN
[dbo].[CustomerCustomerDemo] [LPA_L4] ON [LPA_L3].[CustomerID]=[LPA_L4].[CustomerID])
WHERE
( ( ( ( ( [LPA_L4].[CustomerTypeID] = @p2)))))
with both parameters which is correct but the other variation
Employee.FilterByOrder(1).FilterByCustomerTypeIdViaOrders("ALFKI")
gives
DECLARE @p1 Int; SET @p1=1
DECLARE @p2 NChar(10); SET @p2='ALFKI'
SELECT [LPA_L1].[Address],
[LPA_L1].[BirthDate],
[LPA_L1].[City],
[LPA_L1].[Country],
[LPA_L1].[EmployeeId],
[LPA_L1].[Extension],
[LPA_L1].[FirstName],
[LPA_L1].[HireDate],
[LPA_L1].[HomePhone],
[LPA_L1].[LastName],
[LPA_L1].[Notes],
[LPA_L1].[Photo],
[LPA_L1].[PhotoPath],
[LPA_L1].[PostalCode],
[LPA_L1].[Region],
[LPA_L1].[ReportsTo],
[LPA_L1].[Title],
[LPA_L1].[TitleOfCourtesy]
FROM
((( [dbo].[Employees] [LPA_L1]
INNER JOIN
[dbo].[Orders] [LPA_O2] ON [LPA_L1].[EmployeeId]=[LPA_O2].[EmployeeID])
INNER JOIN
[dbo].[Customers] [LPA_L3] ON [LPA_L3].[CustomerID]=[LPA_O2].[CustomerID])
INNER JOIN
[dbo].[CustomerCustomerDemo] [LPA_L4] ON ( [LPA_L3].[CustomerID] = [LPA_L4].[CustomerID]))
WHERE
( ( ( ( ( [LPA_L4].[CustomerTypeID] = @p2)))))
which is missing the first filter -@p1. If I swap the order it works
Employee.FilterByCustomerTypeId("ALFKI").FilterByOrder(1)
SD.LLBLGen.Pro.LinqSupportClasses.NET35.dll 3.5.12.1026
SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll 3.5.12.1004