views:

50

answers:

1

select ForumCategories.ID , ForumCategories.Title , ForumCategories.DateCreated, CO = ( select COUNT(*) from ForumSubCategories where ForumSubCategories.CategoryID_FK = ForumCategories.ID) from ForumCategories

+2  A: 
var q = from fc in Context.ForumCategories
        select new 
        {
            Id = fc.ID,
            Title = fc.Title,
            DateCreated = fc.DateCreated
            CO = fc.ForumSubCategories.Count()
        };
return q;

The "join" (subquery) is implicit; it's defined in the relationship between ForumCategories and ForumSubCategories in your model. Using this syntax, the call to Count() will be done on the DB server.

Craig Stuntz
and what if i have to pass this result as data source to gridview control
faru
q is `IEnumerable`, which should be enough. But you can project onto a strong type if you need `IEnumerable<SomeViewModel>` http://blogs.teamb.com/craigstuntz/2009/12/31/38500/
Craig Stuntz
Thanks Craig it worked.:)
faru
Craig what if i want to get the dateCreated from ForumSubCategories how can i do that.
faru
"The" `dateCreated`? Aren't there lots of them (since there are lots of ForumSubCategories; otherwise you wouldn't be counting them)? Do you want a list of all the dates? The maximum?
Craig Stuntz
sorry Craig i wasnt able to explain it well. i got three tables ForumCategories (ID, Title, Desc, DateCreated) , ForumSubCategories ID, Title, Desc, DateCreated, CategoryID_FK) and ForumPosts ( ID, Heading, Detail, DateCreated, SubCategoryID_FK). now i want to return FourmCtegories.ID, FourmCtegories.Title , Count of SubCategories and LastPost posted uneder that Category. thanks
faru