C# 如何在一个LINQ查询中连接两个表并使用aggreagate函数

C# 如何在一个LINQ查询中连接两个表并使用aggreagate函数,c#,.net,linq-to-sql,C#,.net,Linq To Sql,我想合并两个表,其中一个表中是分数,另一个表中是关于示例文章的信息 表: Articles --------------- article_id title date category user_id Articles_Scores --------------- article_id user_id score 我已经写了这个linq查询: from p in db.Articles.Where(p => p.user_id == 2) join o in db.Articles_S

我想合并两个表,其中一个表中是分数,另一个表中是关于示例文章的信息

表:

Articles
---------------
article_id
title
date
category
user_id

Articles_Scores
---------------
article_id
user_id
score
我已经写了这个linq查询:

from p in db.Articles.Where(p => p.user_id == 2)
join o in db.Articles_Scores.Where(o => o.user_id == 2) on p.article_id equals o.article_id
group o by o.article_id into result
select new
{
result.Average(m => m.score)
};
如何选择其他文件。为什么我不能在select中使用p?有人能告诉我应该如何做才能得到以下结果:

article_id  title   date    category    score

我建议您加入DBML designer,并使用以下内容:

var query = (from article in articles
            where article.article_id == givenId && article.user_id == givenUser
            let scores = article.scores.ToList()
            from score in scores
            select score.value).Sum();
from p in db.Articles.Where(p => p.user_id == 2)
select new
{
    p.article_id, 
    p.title, 
    p.date, 
    p.category,
    AverageScore = db.Articles_Scores
                     .Where(o => o.user_id == p.user_id && p.article_id == o.article_id)
                     .Average(m => m.score)
};