在LINQ表达中需要帮助,
我有2个表Article
和comments
外键是Article_id
。现在我需要使用linq到sql这两个表的数据,如article_id,Article_title,Total_Comments
(对当前文章文章的评论数量) ),
故事结构:
table 1(Article) : Article_ID,Article_Title.. etc
table 2(comments ): Comment_auto_id,Comment_text,Comment_by(User_ID),Article_ID
答案 0 :(得分:1)
类似的东西?
from article in Articles
join comment in Comments on article.Article_ID equals comment.ArticleID into articleComments
select new {
Article = article,//or more detailed if you want only part of Articles entity
Total_Comments = articleComments.Count()
}
答案 1 :(得分:0)
也许你需要使用.GroupJoin()扩展方法。 资料来源:http://msdn.microsoft.com/en-us/library/system.linq.enumerable.groupjoin.aspx