3.1.11.0706 Final (SD.LLBLGen.Pro.DQE.SqlServer.NET20.dll)
3.1.12.0806 (SD.LLBLGen.Pro.ORMSupportClasses.NET20.dll
3.1.12.0507 (SD.LLBLGen.Pro.LinqSupportClasses.NET35)
DotNet 4.0 vs2010 project
Adapter template
I have reformulated a previous question (http://www.llblgen.com/tinyforum/Messages.aspx?ThreadID=21316) to use Northwind.
I want to select all customers with a Region and have orders that ShipVia Speedy (ShipVia == 1). Then I want to create counts for:
- Total orders
- Total of employees that took these orders
- Sub total of employees that took orders from this set where the order was delivered before the employee was hired. (fraud detection maybe?)
The code I come up with looks like this using a DateTime.Now instead of the o.ShipDate and runs but produces wrong results:
var q = from c in metaData.Customer
join o in metaData.Order on c.CustomerId equals o.CustomerId
where o.ShipVia == 1 && c.Region != null
group c by new {c.CustomerId, c.Region}
into g
select new
{
g.Key.CustomerId,
g.Key.Region,
TotalOrders = g.Count(),
EmployeeCount =
g.SelectMany(h => h.Orders).Where(i => i.CustomerId == g.Key.CustomerId).Select(j => j.Employee)
.Count(),
EmployeeCount2 =
g.SelectMany(h => h.Orders).Where(i => i.CustomerId == g.Key.CustomerId).Select(j => j.Employee)
.Count(k => k.HireDate > DateTime.Now),
};
The counts for Employees are for all orders, not just the set of orders selected in the parent select.
How can I correlate the orders used in the counts with those used in the top level join when I don't get access to the joined entity once I had done the group by? Also, how do I get access to the ShipDate of the joined order to test against the employee's HireDate?