0
class user    
{
    public string userID { get; set; }
    public string groupID { get; set; }
    public int individualCredit { get; set; }
    public int groupCredit { get; set; }
}

I have a list like this

 List<user> listUsers = new List<user>();

I need to do following things and I'm giving what I have tried so far.

I want to group users with same groupID.

Calculate groupCredit by adding the individualcredit s of each member in group and by dividing it by the number of group members.

Finally I want to assign each user with their groupCredit. There are groups with three to five members.

Can anyone help me in this? at least give me a sample solved question? I searched but I didn't find anything quite matching with this.

This is my linq so far

var groups = lstUsers.GroupBy(x => x.groupID).Select(g => new {ID=g.Key,count=g.Count() });

Here the thing I don't understand to do is get the group mark (It's calculated adding all group members marks and dividing it by number of group members) and to assign group mark to each member in a group.

ekad
  • 14,436
  • 26
  • 44
  • 46
Sahil
  • 137
  • 1
  • 1
  • 12

3 Answers3

1
var groups = listUsers.GroupBy(x => x.groupID)
                .Select(g => new { ID = g.Key, AverageCredit = g.Average(u => u.individualCredit) });

foreach (var user in listUsers)
    user.groupCredit = groups.First(u => u.ID == user.groupID).AverageCredit;
Edin
  • 1,476
  • 11
  • 21
  • if I have variable like this in my class public int groupPercentage { get; set; } and If I want to calculate groupPercentage=(individuaMark/groupMark)*100; How to do that? it's enough if you can show how to access individual mark. Thanks – Sahil May 24 '14 at 02:58
  • You could add that to the foreach loop. You can access to the user properties there. I see that you could be quite novice to the programming. A word of advice: just try stuff out. Do it the way you might think it could work and give it a shot. Use a debugger, step through your code and use watcher to see how your variables look like. In this assignment it is pretty much clear what is right and what is wrong. You might get it wrong a couple of times, but you will most definitely learn from it. – Edin May 24 '14 at 09:03
0

You do all querying in one LINQ expression, and then set everything in a loop.

var credits = lstUsers.GroupBy(x => x.groupID)
        .Select(g => {ID = g.Key, Credit = g.Sum(x => x.individualCredit)/g.Count()});

foreach (var user in lstUsers)
{
    user.groupCredit = credits.Single(x => x.ID == user.groupID).Credit;
}
Andrei
  • 55,890
  • 9
  • 87
  • 108
  • if I have variable like this in my class public int groupPercentage { get; set; } and If I want to calculate groupPercentage=(individuaMark/groupMark)*100; How to do that? it's enough if you can show how to access individual mark. Thanks – Sahil May 24 '14 at 03:03
0

Since classes are by reference you can just add that to the anonymous type. then you don't have to relookup by the key

var listUsers = new List<user>();
var groups = listUsers.GroupBy(x => x.groupID).Select(g => new { users = g, groupCredit = g.Average(u => u.individualCredit) });
foreach (var group in groups)
{
    foreach (var member in group.users)
    {
        // Avg is a decimal have to cast back to int for class
        member.groupCredit = (int)group.groupCredit;
    }
}
CharlesNRice
  • 3,219
  • 1
  • 16
  • 25