Getting a Repeater/GridView to show child values of relationships with Entities

Once again, it is a bit more involved than L2S, but relatively simple

I have made L2S my go-to for a while now, and I will keep referring back to it as a baseline of how ORMs *should* work. When I am using a repeater, and I want to show a related object value, I can simply do this with L2S (same syntax for L2E as well):
<%# Eval("state.state_name") %>

Where 'state' is a related object. Similarly, if I try the same thing in L2E, it will not error out, it will just show up blank (I would much prefer an error). Like I said, the syntax is the exact same for that part, it is just different when you are getting your data. You now need to explicitly include what child objects you want to load with your data. So, for the above example, I could something like this:

linq-to-sql
var results = from p in db.records select p;
Repeater1.DataSource = results;
Repeater1.DataBind();

But, to get anything other than blanks to show up with L2E, I need to change it a bit:

linq-to-entities
var results = from p in db.records.Include("state") 
  select p;
Repeater1.DataSource = results;
Repeater1.DataBind();

Now it knows that 'state' has to be loaded with the original records. You can chain mulitple Include()s along to include more and more stuff as well:

linq-to-entities
var results = from p in 
  db.records.Include("state").Include("city")
  select p;

Add comment

Loading