使用LINQ to加入,集团通过和Count ....面临麻烦 [英] Using Linq to Join,Group By and Count.... facing a trouble
问题描述
我有两个表,图片
和 pictureratings
在我的分贝。
I have two tables, picture
and pictureratings
in my db.
public partial class picture
{
public int idpicture { get; set; }
public int iduser { get; set; }
public string picTitle { get; set; }
public string picFilename { get; set; }
public System.DateTime pictime { get; set; }
public int nuditylevel { get; set; }
public int fakeslevel { get; set; }
// This property will hold the total accumulated/summed
// up rating of a picture
public int totalrating { get; set; }
}
public partial class pictureratings
{
public int idrating { get; set; }
public int idpictures { get; set; }
public int iduser { get; set; }
public System.DateTime iddatetime { get; set; }
public int iduserrateddby { get; set; }
public int rating { get; set; }
}
对于每一个等级排将创建一个新的 pictureratings。我想通过照片ID进行分组
pictureratings
表再算上喜欢。我想,以显示图片
表那些喜欢在 totalrating
属性。
For every rating a new pictureratings
row will be created. I want to group pictureratings
table by the picture id and then count the likes. I want to show those likes in picture
table in totalrating
property.
所以,按照我的研究到现在,我可以写这篇code
So as per my research till now, I am able to write this code
var total = from p in db.picturedetails
join l in db.picturelikes on p.idpictures equals l.idpictures
group l by l.idpictures into g
select new
{
IdUserPic = g.First().iduser,
IdPictures = g.First().idpictures,
totalrating = g.Count()
}
我使用的网页API返回的查询总量。所以这才是我的问题:如何显示图片
如 ID用户
, picTitle 性能code>,
picFilename
, pictime
, nuditylevel
和 fakeslevel
?我这样做对吗?我应该怎么办呢?
I am using web api to return query total. So here comes my problem: how to show the picture
properties like iduser
, picTitle
, picFilename
, pictime
, nuditylevel
and fakeslevel
? Am I doing it right? How should I do it?
推荐答案
您可以组一个匿名类。这样,你可以访问这两个P与M.升特性。结果
看起来是这样的:
You could group an anonymous class. That way you can access both p & l properties.
Looks like this :
var total = from p in db.picturedetails
join l in db.picturelikes on p.idpictures equals l.idpictures
group new {p,l} by l.idpictures into g
select new
{
IdUserPic = g.First().p.iduser,
IdPictures = g.First().p.nuditylevel,
totalrating = g.Count()
}
这篇关于使用LINQ to加入,集团通过和Count ....面临麻烦的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!