0

I have 3 entities

My Entities I'd like to flatten the tblCondition and BusinessAreas into one object. What I want is Category.ID, Category.Category, BusinessArea.ID, BusinessArea AreaName.

I know this can be done by creating an Anonymous type with Lambda but I'm relatively unskilled with Lampda or LINQ.

Forgot to mention that I need to get to the two tables through the first one.

My original call looks like this.

myConditionTemplate = EE.ConditionTemplates.Where(c => c.TemplateCode == TextBoxSearchConditionCode.Text).FirstOrDefault();
Nathan Ernst
  • 4,540
  • 25
  • 38
Tyddlywink
  • 876
  • 7
  • 28

1 Answers1

1

Here's the official documentation: http://msdn.microsoft.com/en-us/library/vstudio/bb384105.aspx

Essentially in your select portion use the new keyword without a class name like:

select new { Category.ID, Category.Category, BusinessArea.ID, BusinessArea.AreaName }

The webpage only shows an example using Linq in query form, but to do it in method form:

var results = db.GetStuff().Select(x => new { x.ID, x.Name });

Ack, that may not be very clear. I just found some great examples at How to do a join in linq to sql with method syntax?. This was a different question, but the answer's example shows you how to do the lamba for a joined enumerable set.

UPDATE: Since you updated your question, see if this helps:

var results = myConditionTemplate.Select(x => new { CategoryID = x.tblCondition.ID, Category = x.tblCondition.Category, BusinessAreaID = x.tblCondition.BusinessArea.ID, AreaName = x.tblCondition.BusinessArea.AreaName});
Community
  • 1
  • 1
Geoff Gunter
  • 151
  • 1
  • 10
  • 1
    By the way, I learned a TON from Tekpub.com's tutorial series on Mastering Linq. Shamelessly I'll post my referral code: http://tekpub.refr.cc/F7B888K. :) But either way, check them out. Tons of great stuff. – Geoff Gunter Jul 26 '13 at 21:06