I want to create a sorted list of users that appear in one datatable “StatsTable”.
I need to get the userID from StatsTable and then use this to find the UserName in another datatable “UserTable”. When I have these I want to make the userID the Key and the UserName the Value in the sorted list.
I managed to get the userID and add it to the sorted list but the userName is coming through as “System.Data.EnumerableRowCollection`1[System.String]”.
What am I doing wrong? Thanks for your help.
SortedList UserList = new SortedList();
List<double> listofUserIDs = StatsTable.AsEnumerable()
.Select(uid => uid.Field<double>("UserID")).ToList<double>();
foreach (double UID in listofUserIDs)
{
string userName = UserTable.AsEnumerable()
.Where(id => double.Equals(id.Field<double>("UserID"), UID))
.Select(name => name.Field<string>("First_Name") + " " + name.Field<string>("Last_Name")).ToString();
UserList[UID] = userName;
}
You should use
Enumerable.Jointo link both tables and select what you need. In this case i would use aTuple<int, string>whereas theintis the UserID(a UserID with decimal places makes no sense, does it?) and the string is the username:You can access a tuple via the
Itemproperties: