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>();
  1. I want to group users with same groupID.

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

  3. Finally I want to assign each user with their groupCredit.

There are groups with three to five members.

How to do that?

Here's what I have tried.

var groupedUsers = from users in listUsers 
                   where users.groupID != "" 
                   group users by users.groupID into grouphold 
                   select grouphold ;

Here's what I don't know.

How to search for the same groupID? In what I have tried I check for all Items groupID is blank.

ekad
  • 14,436
  • 26
  • 44
  • 46
Sahil
  • 137
  • 1
  • 1
  • 12
  • 1
    "How to do that?" - With a little research and effort! Try it out and come back when you're stuck with something specific. Don't ask before you've even tried. – Neil Smith May 23 '14 at 15:51
  • 3
    Smells like homework - show some effort and then come back. – Preston Guillot May 23 '14 at 15:56
  • @Sahil Thank you for posting your attempt. It looks like a good start! If the groupID is blank though, that means the data itself is no good. Perhaps look at where these items are getting populated and see why it isn't correct. – BradleyDotNET May 23 '14 at 16:11
  • @BradleyDotNET Here I searched for a groupID value but I want to group matching groupID s and get the group member count. I have NO idea in that. – Sahil May 23 '14 at 16:14
  • @Sahil, so the groupIDs are OK? We can help with the counting piece :) – BradleyDotNET May 23 '14 at 16:20
  • Your existing query is doing most of what you're asking for (@BradleyDotNET is pointing out that your initial data may be loaded incorrectly) - it's creating groupings of `user` instances that share a common `groupID`. If you want to see the value of a group's key, you can select the key from any member of the group. If you want to see how many elements are in any particular group, you can iterate the group and count them, or preferably you can use the LINQ `Count` method, which does just that. I'll leave it as an exercise to you to complete these steps, but you're on the right track! – Preston Guillot May 23 '14 at 16:20
  • @PrestonGuillot I'd like to see any code help. because I have no idea how to seperate each groupID other than manually assigning a one. – Sahil May 23 '14 at 16:36

1 Answers1

0

Althought I agree with Sahil's comment, here is a starting point...

You can use LINQ, something like this:

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

    public int groupUsersbyID(int _groupID)
    {

       var ListOfUsers = (from g in listUsers where g.groupID == _groupID select g).ToList();
       var count = ListOfUsers.Count();
       var totalCreditforGroup = 0;
       var GroupCredit = 0;

       foreach (var indidCredit in ListOfUsers)
        {
            totalCreditforGroup = totalCreditforGroup + indidCredit.individualCredit;

            GroupCredit = totalCreditforGroup / count;
        }

       return GroupCredit;
    }


    public int returnGroupID(int userId)
    {
        var GroupIDforUser = (from i in listUsers where i.userID == userId select i.groupID).FirstOrDefault();

        return GroupIDforUser;
    }

    public class user
    {
        public int userID { get; set; }
        public int groupID { get; set; }
        public int individualCredit { get; set; }
        public int groupCredit { get; set; }
    }
Matt D. Webb
  • 3,216
  • 4
  • 29
  • 51
  • I need to insert the functionality in to a button. This is much a complexed scenario. I might sound like stupid. But do I have to place this codings in my class.cs file? Or main form publicly where I have declared my list? – Sahil May 23 '14 at 16:41
  • You can call the functions in your button events, assuming your project is classic asp or a winforms app etc. In your button event you call simply call either function: returnGroupID(ID) groupUsersbyID(ID) You will need to make a reference to the cs file this code is located. The 'listUsers' is defined globally within the cs namespace so this can be called anywhere within it to be iterated through. – Matt D. Webb May 27 '14 at 07:12