I want to merge two tables where in one table is scores and in another table are i开发者_如何学Gonformation about for example article.
Tables:
Articles
---------------
article_id
title
date
category
user_id
Articles_Scores
---------------
article_id
user_id
score
I already write this linq query:
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)
};
How can I select the other fileds. Why I cant use p in select? Can someone tell how should I do this to get following results:
article_id title date category score
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)
};
I suggest that you join them in DBML designer and use this:
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();
精彩评论