C# 增量用户';基于用户增加的s点';s职位
我有一个简单的用户,具有以下特征--C# 增量用户';基于用户增加的s点';s职位,c#,asp.net-mvc,algorithm,linq,linq-to-entities,C#,Asp.net Mvc,Algorithm,Linq,Linq To Entities,我有一个简单的用户,具有以下特征-- public class User { public User() { this.Posts = new HashSet<Post>(); } [Key] public int UserId { get; set; } public string UserName { get; set; } public string Password { get
public class User
{
public User()
{
this.Posts = new HashSet<Post>();
}
[Key]
public int UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
piblic int point {get; set;}
public virtual ICollection<Post> Posts { get; set; }
}
}
public class Post
{
public Post()
{
public int PostId { get; set; }
public string Message { get; set; }
public int PostedBy { get; set; }
public virtual User User { get; set; }
}
}
现在,用户和posts表之间存在一对多关系。1个用户可以有多个帖子。现在,我想根据用户在帖子表中的帖子增量增加用户的分数。假设一个用户在某些问题上发布了5篇帖子,那么我想将他的分数从0增加到25,即每篇帖子的分数,增量为5。
到目前为止,我尝试的是这个问题-----
1> 现在,我想将这个总计数值(输出)分配给UserProfilesTable中用户的point列,并将该值保存到数据库中。
多谢各位 你不需要将计数乘以5吗?@juharr不,如果使用乘法,那么如果posts.count是3,那么输出将是25。我只想在每个post的增量上增加计数器值5。如果计数是3,而你乘以5,那么输出将是15。为什么你认为会是25?似乎如果每篇文章值5分,那么你的总分就是5分。如果这不正确,你能解释为什么吗?@juharr Ohhh抱歉,guy你的建议是对的。这可能适合这里,但实际上,我有一个场景,用户的分数随着帖子、评论、喜欢等数量的增加而增加。我希望使用Linq方法处理嵌套查询。顺便说一句,非常感谢你的帮助@Robert McKeeI已经用一个LINQ查询更新了答案,假设你已经设置了正常的导航属性,那么这个查询应该适合你。你能再告诉我一件事吗?如何根据用户帖子评论数量的增加来提高用户的分数。如何适应这一点。那会解决我所有的问题。提前感谢
point=up.Posts.Count()*5+up.Posts.Sum(p=>p.Comments.Count())
public class User
{
public User()
{
this.Posts = new HashSet<Post>();
}
[Key]
public int UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public int point {get { return this.Posts.Count*5; } }
public virtual ICollection<Post> Posts { get; set; }
}
public class User
{
public User()
{
this.Posts = new HashSet<Post>();
}
[Key]
public int UserId { get; set; }
public string UserName { get; set; }
public string Password { get; set; }
public int point {get { return this.Posts.Count*5; } }
public virtual ICollection<Post> Posts { get; set; }
}
var user = db.UserProfiles.Select(up=> new User {
UserId=up.UserId,
UserName=up.UserName,
Password=up.Password, //Really?
point=up.Posts.Count()*5,
});