Spoke to soon. It starts to break when you set criteria for a joined object.
So I decided to test this with Linq to SQL. Turns out this all works in Linq to SQL. So I think I have found a bug.
Here is what I have as my test repository in Linq to SQL
public IQueryable<Data.Contact> GetQueryable()
{
var db = new LinqToSqlDataContext();
var qry = db.Contacts.Select(ContactEntityMap());
return qry.AsQueryable();
}
public Func<Contact, Data.Contact> ContactEntityMap()
{
return (c => new Data.Contact
{
ID = c.EntityIndex.Identifier,
Created = c.EntityIndex.Created,
Modified = c.EntityIndex.Modified,
ObjectType = (EntityType)c.EntityIndex.TypeID,
EmailHome = c.EmailHome,
FirstName = c.FirstName,
LastName = c.LastName,
MiddleName = c.MiddleName,
PhoneHome = c.PhoneHome,
Salutation = c.Salutation,
Suffix = c.Suffix,
Title = c.Title,
HomeAddress = c.EntityIndex.Addresses
.Where(a => a.Identifier == c.Identifier
&& a.AddressTypeID == Convert.ToInt32(AddressType.Home))
.Select(AddressEntityMap()).Single()
});
}
public Func<Address, Data.Address> AddressEntityMap()
{
return (a => new Data.Address
{
ID = a.Identifier,
AddressType = (AddressType)a.AddressTypeID,
Attention = a.Attention,
Street1 = a.Street1,
Street2 = a.Street2,
City = a.City,
StateOrProvince = a.StateOrProvince,
PostalCode = a.PostalCode,
Country = a.Country
});
}
Here is the tests I run on both a LLBLGen example of this and the Linq to SQL
var lqry1 = linqtoSQL.GetQueryable();
var lqry3 = lqry1.Where(p => p.HomeAddress.AddressType == AddressType.Home).ToList();
var lqry2 = lqry1.Where(p => p.ID == entity.ID).ToList();
This above code and test works for Linq to SQL. I have the exact same scenario setup for LLBLGen which fails. Also, this scenario fails for LLBLGen even if I don't use my mapping functions above.
Test method LeadManager.DataTests.ContactTests.Contact_Query_Random threw exception: SD.LLBLGen.Pro.ORMSupportClasses.ORMQueryExecutionException: An exception was caught during the execution of a retrieval query: An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name.. Check InnerException, QueryExecuted and Parameters of this exception to examine the cause of this exception. ---> System.Data.SqlClient.SqlException: An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as "" or [] are not allowed. Change the alias to a valid name..
This error was generated when running the query against LLBLGen. This only occurs when I run a where against the joined object as in above.
This is a game changer for my scenario. I want to be able to map my domain objects and still return a real IQueryable. I cannot return a List that is IQueryable becuase I would always pull back all records. There are millions of records.
Bryan