Pulling records from a referenced table using Entity Framework

Strangely, most of Entity Framework examples are for just one table. In real world, it is very rare, unless you use some document database. Often you get a result set from a database which joins two or three tables.

There is one example of how to use navigation property.

I tried it, and thanks to ReSharper, I learned that I could use “let” linq statement, if you fetch only one row

my code changed like this.

using (var context = new PLog())
{
 var prcs = (from p in context.PTableSet
 where p.UId == UId
 let c = p.Customer.FirstOrDefault()
 select new Pricing()
 {
 Ec = p.Ec,
 Fv = p.Fv,
 RoEc = p.RoEc,
 RoRwa = p.RoRwa,
 Rwa = p.Rwa,
 UId = p.UId,
 VD = p.VD,
 Customer = new Customer() {Name = c.Name},
 }
 ).ToList();

 return prcs.ToList();
}
Pulling records from a referenced table using Entity Framework

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s