I am new to LINQ and cannot plunge into any kind of inconsistency in behavior. Any knowledgeable materials would be highly appreciated. I see similar issues in SO and elsewhere, but they don't seem to help.
I have a very simple setup - a company table and an address table. Each company can have 0 or more addresses, and if> 0, you must specify the main address. I am trying to handle cases where there are 0 addresses using an external connection and changing the select statement accordingly.
Note that I am currently binding the output directly to the GridView, so I would like to keep all the processing in the request.
The following DOES work
IQueryable query =
from comp in context.Companies
join addr in context.Addresses on comp.CompanyID equals addr.CompanyID into outer // outer join companies to addresses table to include companies with no address
from addr in outer.DefaultIfEmpty()
where (addr.IsMain == null ? true : addr.IsMain) == true // if a company has no address ensure it is not ruled out by the IsMain condition - default to true if null
select new {
comp.CompanyID,
comp.Name,
AddressID = (addr.AddressID == null ? -1 : addr.AddressID), // use -1 to represent a company that has no addresses
MainAddress = String.Format("{0}, {1}, {2} {3} ({4})", addr.Address1, addr.City, addr.Region, addr.PostalCode, addr.Country)
};
GridView ", , ()"
, MainAddress,
MainAddress = (addr.AddressID == null ? "" : String.Format("{0}, {1}, {2} {3} ({4})", addr.Address1, addr.City, addr.Region, addr.PostalCode, addr.Country))
Could not translate expression spewey , .
, MainAddress, AddressID, - , ?
.