开发者

Get Average Using LINQ and Join Multiple Tables

StackOverFlow helped me on a different post to construct the LINQ below. How can I modify the LINQ to apply a second where condition and join a second table? The second where condition will be looking at a column on the second table.

var ratingAverage开发者_如何学编程 = ctx.Rates.Where(r => r.Id == Id)  
    .GroupBy(g => g.Id, r => r.Rating)  
    .Select(g => new { Id = g.Key, Rating = g.Average() });

The LINQ above will get the average and group by the Id. I.E., I have a people table. I want to include the People table in the LINQ and enhance the Where condition to filter on PersonId. The Id on the Rating table is a Foreign Key on the People table.

Thanks so much for the help given.


int ratingId = 1; 
int personId = 9;

var ratingAverage = ctx.Rates.Where(
     r => r.Id == ratingId && r.Person.Id == personId)  
   .GroupBy(g => g.Id, r => r.Rating)  
   .Select(g => new { Id = g.Key, Rating = g.Average() });
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜