As part of my efforts to Linqafy http://www.codeplex.com/RapidDevBookCode I attempted a Linq version of a TypedList
var customerlist = from customer in AWHelper.MetaData.Customer
from customerAddress in customer.CustomerAddress
select new
{
customerAddress.Address.AddressLine1,
customerAddress.Address.AddressLine2,
customerAddress.Address.City,
AddressType = customerAddress.AddressType.Name,
customer.Individual.Contact.Title,
customer.Individual.Contact.FirstName,
customer.Individual.Contact.MiddleName,
customer.Individual.Contact.LastName,
customer.Individual.Contact.Suffix,
customer.Individual.Contact.EmailAddress,
customer.Individual.Contact.EmailPromotion,
CountryRegionName = customerAddress.Address.StateProvince.CountryRegion.Name,
StateProvinceName = customerAddress.Address.StateProvince.Name,
customer.CustomerId
};
if (MaxNumberOfItemsToReturn > 0)
customerlist = customerlist.Take(MaxNumberOfItemsToReturn);
bindingSource1.DataSource = customerlist;
When I ran it I got a SQL error; Here is the trace:
An exception was caught during the execution of a retrieval query: The multi-part identifier "LPLA_4.AddressLine1" could not be bound.
The multi-part identifier "LPLA_4.AddressLine2" could not be bound.
The multi-part identifier "LPLA_4.City" could not be bound.
The multi-part identifier "LPLA_5.Name" could not be bound.
The multi-part identifier "LPLA_7.Title" could not be bound.
The multi-part identifier "LPLA_7.FirstName" could not be bound.
The multi-part identifier "LPLA_7.MiddleName" could not be bound.
The multi-part identifier "LPLA_7.LastName" could not be bound.
The multi-part identifier "LPLA_7.Suffix" could not be bound.
The multi-part identifier "LPLA_7.EmailAddress" could not be bound.
The multi-part identifier "LPLA_7.EmailPromotion" could not be bound.
The multi-part identifier "LPLA_9.Name" could not be bound.
The multi-part identifier "LPLA_8.Name" could not be bound.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception.
: Initial expression to process:
value(SD.LLBLGen.Pro.LinqSupportClasses.DataSource`1[AW.Data.EntityClasses.CustomerEntity]).SelectMany(customer => customer.CustomerAddress, (customer, customerAddress) => new <>f__AnonymousType1`14(AddressLine1 = customerAddress.Address.AddressLine1, AddressLine2 = customerAddress.Address.AddressLine2, City = customerAddress.Address.City, AddressType = customerAddress.AddressType.Name, Title = customer.Individual.Contact.Title, FirstName = customer.Individual.Contact.FirstName, MiddleName = customer.Individual.Contact.MiddleName, LastName = customer.Individual.Contact.LastName, Suffix = customer.Individual.Contact.Suffix, EmailAddress = customer.Individual.Contact.EmailAddress, EmailPromotion = customer.Individual.Contact.EmailPromotion, CountryRegionName = customerAddress.Address.StateProvince.CountryRegion.Name, StateProvinceName = customerAddress.Address.StateProvince.Name, CustomerId = customer.CustomerId)).Take(5)
Method Enter: CreatePagingSelectDQ
Method Enter: CreateSelectDQ
Method Enter: CreateSelectDQ
Generated Sql query:
Query: SELECT DISTINCT TOP 5 [LPLA_4].[AddressLine1], [LPLA_4].[AddressLine2], [LPLA_4].[City], [LPLA_5].[Name] AS [AddressType], [LPLA_7].[Title], [LPLA_7].[FirstName], [LPLA_7].[MiddleName], [LPLA_7].[LastName], [LPLA_7].[Suffix], [LPLA_7].[EmailAddress], [LPLA_7].[EmailPromotion], [LPLA_9].[Name] AS [CountryRegionName], [LPLA_8].[Name] AS [StateProvinceName], [LPA_L1].[CustomerID] AS [CustomerId] FROM ( [AdventureWorks].[Sales].[Customer] [LPA_L1] INNER JOIN [AdventureWorks].[Sales].[CustomerAddress] [LPA_L2] ON ( [LPA_L1].[CustomerID] = [LPA_L2].[CustomerID]))
Method Exit: CreateSelectDQ
Method Exit: CreatePagingSelectDQ: no paging.
To reproduce this using the project code run the app then open the Customers form then press the ‘Linq Barf’ button.
Strangely I can get this to work by adding a meaningless where clause e.g.
var customerlist = from customer in AWHelper.MetaData.Customer
from customerAddress in customer.CustomerAddress
where customerAddress.Address.AddressLine1 == customerAddress.Address.AddressLine1
select new…
DB is Adventure Works(2005)