O'Reilly Forums: Can We Use Iqueryable Results For Projection - O'Reilly Forums

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

Can We Use Iqueryable Results For Projection

#1 User is offline   GregJF 

  • New Member
  • Pip
  • Group: Members
  • Posts: 4
  • Joined: 12-August 09

Posted 02 September 2012 - 03:26 PM

Folks

We have an entity (Company) that has 30+ related entities. We use projection to query on these relationships.

For example: we have these entity relationships Company->CompanyDetail->Country AND Company->CompanyContact->CompanyContactDetail

So when we need to get Company details and the contact details we use projection like this:
mycontext.Companies.Where(c => c.CompanyID == 123).Select(c => new
				{
					Company = c,
					CompanyDetails = c.CompanyDetails.Where(d => d.LanguageID == 61).Select(cd => new
					{
						CompanyDetail = cd,
						Country = cd.Country
					}).FirstOrDefault(),
					CompanyContacts = c.CompanyContacts.Select(cc => new
					{
						CompanyContact = cc,
						CompanyContactDetail = cc.CompanyContactDetails.Where(d => d.LanguageID == 61)
					}).AsEnumerable().Select(c1 => c1.Company);

But as we have 30+ relationships that all rely upon the relationship Company->CompanyDetail->Country, we would like to break down the queries so that they could be more reusable(less chance of error)

We envisage this type of scenario:
Company GetCompanyContacts( int CompanyID, int LanguageID )
{
	Company company;
	using (Context  mycontext = new Context)
	{
	IQueryable<Company> companyDetails = GetCompany(CompanyID, LanguageID );
	IQueryable<Company> companyContact = GetCompanyContact(CompanyID, LanguageID );
	// some how combine into a protection of company and ContactDetails
	company = companyDetails...??
	}
	return company;
}



IQueryable<Company> GetCompany( int CompanyID, int LanguageID )
{
	return mycontext.Companies.Where(c => c.CompanyID == CompanyID).Select(c => new
							{
								Company = c,
								CompanyDetails = c.CompanyDetails.Where(d => d.LanguageID == LanguageID).Select(cd => new
								{
									CompanyDetail = cd,
									Country = cd.Country									
								}).FirstOrDefault()
						).AsEnumerable().Select(s => s.Company);
}

IQueryable<Company> GetCompanyContact( int CompanyID, int LanguageID )
{
	return mycontext.CompanyContacts.Where(c => c.CompanyID == CompanyID).Select(cc => new
					{
						CompanyContact = cc,
						CompanyContactDetail = cc.CompanyContactDetails.Where(d => d.LanguageID == LanguageID)
					}).AsEnumerable().Select(c1 => c1.Company);

}


This way we would have 30+ IQueryable methods that could be combined as needed

Our question is How do we join them so that they can preform a projection query?

Kindest Regards

GregJF
0

#2 User is offline   JulieLerman 

  • Advanced Member
  • PipPipPipPipPipPipPipPip
  • Group: O'Reilly Author
  • Posts: 334
  • Joined: 17-September 08

Posted 02 December 2013 - 07:53 PM

This forum is for questions on the Programming Entity Frameowrk book. Best bet with general EF qeustions is going to be Stackoverflow.com or MSDN forums where there are thousands of participants.
0

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • This topic is locked

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users