随笔 - 547  文章 - 213 评论 - 417 阅读 - 107万

 

From: https://sharepoint.stackexchange.com/questions/194197/how-to-manipulate-likeby-nooflikes-ratedby-averagerating-userrating-using-cs

Copy below:

 

Gone through the problem an got the relevant answer also made blog on it so visit the blog post

Some of the important aspects covered are described breifly as follows.

To rate an Item for the first time by a user the following fields should be modified with the respective field values:

  1. This field stores the number of users who have rated the item its value should be incremented.

  2. A field which stores the user ratings in CSV string format should add the new users rating like.

    int NewRating = 4; //any desired number from 1 to 5.

    item["Ratings"] += NewRating + ",";

  3. This field stores the average of the ratings given by all the rated users. Simple weighted average calculation formulas can be implemented which is

    float OldAverage = item["AverageRating"] == null ? 0 : float.Parse(item["AverageRating"].ToString());

    int OldNumberOfRatings = item["RatingCount"] == null ? 0 : int.Parse(item["RatingCount"].ToString());

    int NewNumberOfRatings = OldNumberOfRatings + 1;

     

    float NewAverage = OldAverage + ( ( NewRating - OldAverage ) / NewNumberOfRatings);

    //Or.

    float NewAverage = ( ( OldAverage * OldNumberOfRatings ) + NewRating ) / NewNumberOfRatings;

     

    item["AverageRating"] = NewAverage;

  4. This field stores the user information in form of array of FieldUserValue which can be modified using following code

    FieldUserValue[] ratedUsers = item["RatedBy"] as FieldUserValue[];

    //where ratedUsers becomes an array of users who have already rated.

    List<FieldUserValue> newUsersRated = new List<FieldUserValue>();

    if (ratedUsers != null)

    foreach (FieldUserValue ratedUser in ratedUsers)

    newUsersRated.Add(ratedUser);

    newUsersRated.Add(FieldUserValue.FromUser(user.LoginName));

    item["RatedBy"] = newUsersRated;

To Rerate the item for a user who has already rated it before you need to first find the user index (say int userIndex) in the item["RatedBy"] field then change the following fields:

  1. Change the user rating in this CSV string by use of userIndex.

    FieldUserValue[] ratedUsers = item["RatedBy"] as FieldUserValue[];

    for (int i = 0; i < ratedUsers.Length; i++)

    if (ratedUsers[i].LookupValue == user.Title)

    int userIndex = i;

    string[] userRatings = item["Ratings"].split(',');

    int OldRating = int.Parse(userRatings[userIndex]);

    int NewRating = 3; //any desired number from 1 to 5.

    userRatings[userIndex] = NewRating.ToString();

    string ratings = userRatings.Join(',', userRatings);

    item["Ratings"] = ratings;

  2. change the average value using formula

    float OldAverage = item["AverageRating"];

    int NumberOfRatings = item["RatingCount"];

    float NewAverage = OldAverage + ( ( NewRating - OldRating ) / NumberOfRatings );

    item["AverageRating"] = NewAverage;

posted on   今夜太冷  阅读(331)  评论(0编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 地球OL攻略 —— 某应届生求职总结
· 提示词工程——AI应用必不可少的技术
· Open-Sora 2.0 重磅开源!
· 周边上新:园子的第一款马克杯温暖上架
点击右上角即可分享
微信分享提示