喜IM計算基礎上,他們已經收到了他們提交的職位,有收到點upvotes,他們已經提交了事件和評論的用戶點 -LINQ to SQL中,進行一次DB調用,而不是三個
public int GetUserPoints(string userName)
{
int? postPoints = db.Posts.Where(p => p.Aspnet_User.UserName == userName).Sum(b => (int?)b.UpVotes);
int? eventPoints = db.Events.Where(p => p.Aspnet_User.UserName == userName).Sum(b => (int?)b.UpVotes);
int? commentPoints = db.Comments.Where(p => p.Aspnet_User.UserName == userName).Sum(c => (int?)c.UpVotes - (int?)c.DownVotes);
return (postPoints.HasValue ? postPoints.Value : 0) + (eventPoints.HasValue ? eventPoints.Value : 0) + (commentPoints.HasValue ? commentPoints.Value/5 : 0);
}
我做了3個獨立的數據庫調用來實現這一點。我可以在一個這樣做嗎?
只是一個提示:使用`postPoints ?? 0'而不是`(postPoints.HasValue?postPoints.Value:0)` – 2011-02-05 11:54:31