views:

24

answers:

1

I have a People table, I want a method that return one Person by id, with numbers of Cars, number of houses, etc.

I need to Load this tables together? I see SQL generated by EF, is a monster.

Ex:

public Person Get()
{
    return context.People.Include("Cars").Include("Houses").Where(x=> x.Id = 1).First();
}

My view use this:

Name: <%= people.Name%>
Cars: <%= people.Cars.Count%>
Houses: <%= people.Houses.Count%>
+1  A: 

You can do this

var result = (
   from p in ctx.People
   where p.Id == 1
   select new {
      Person = p, 
      Cars = p.Cars.Count(), 
      Houses = p.Houses.Count()
   }).FirstOrDefault();

Which will just bring back the counts.

Alex James
The problem is that I cant return "var". I will need create a PeopleXXXModelView
Fujiy
So do `select new PeopleXXXModelView {` instead of `select new {`
Craig Stuntz