Pulling records from a referenced table using Entity Framework

less than 1 minute read

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.

[sourcecode language="csharp"]
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},

return prcs.ToList();