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
2010-01-12 17:56:58
and what if i have to pass this result as data source to gridview control
faru
2010-01-12 18:05:30
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
2010-01-12 18:21:12
Thanks Craig it worked.:)
faru
2010-01-13 10:38:47
Craig what if i want to get the dateCreated from ForumSubCategories how can i do that.
faru
2010-01-13 15:12:57
"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
2010-01-13 15:47:39
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
2010-01-13 17:04:11