A Programmer
A Programmer

Reputation: 655

How do I get count of comments for one news?

I have a table in database : TBL_News and another one : TBL_Comments Now I can show news and comments but I want to show counts of each news in one gridview. I have a gridview on the page that shows last news (title of news) i want to show them like this :

grdNews.DataSource = 
    (from n in context.TBL_News
    join nc n context.TBL_Categories on p.CategoryID equals nc.CategoryID 
    select new {n.NewsID, n.NewsTitle,nc.CategoryTitle});

grdNews.DataBind();

My first news - Posted in : Sport News, By : Admin, Comments:25

That 25 is counts of this news.

How do I get counts of comments for each news?

Upvotes: 1

Views: 81

Answers (2)

A Programmer
A Programmer

Reputation: 655

I got it!

just use another select query in this query :

grdNews.DataSource = 
(from n in context.TBL_News 
join nc n context.TBL_Categories on p.CategoryID equals nc.CategoryID
 select new 
{n.NewsID,
 n.NewsTitle,
 nc.CategoryTitle,
 CommentsCount = 
 (from c in context.TBL_Comments where c.NewsID == n.NewsID select c).Count()
});

Upvotes: 2

amir moradifard
amir moradifard

Reputation: 353

If they are related together by NewsID you can filter TBL_Comments upon loaded NEWS from TBL_News. Its easier to have a DataModel and manage everything by say LINQ.

Upvotes: 0

Related Questions