Reputation: 3
EDIT: Everything that I had problems with was solved in the comments :)
Before we get into things, I'm very new to programming with c#, only had very brief experience with c++. So I'm very sorry if I get some terminology wrong, they are all still confusing to me and I always mix them up.
Alright, so I have a list of basketball players in a separate .csv file. Here's the class for them:
//Renaldas Seibutis;196;AG;Kauno „Žalgiris“;1985-07-23;TRUE;FALSE; <- example of player's data in the .csv file
namespace Lab1
{
class PlayerList
{
public string FullName { get; set; }
public int Height { get; set; }
public string Position { get; set; }
public string Club { get; set; }
public DateTime BirthdayDate { get; set; }
public bool IsInvited { get; set; }
public bool IsCaptain { get; set; }
public PlayerList()
{
}
public PlayerList(string fullName, int height, string position, string club, DateTime birthdayDate, bool isInvited, bool isCaptain)
{
FullName = fullName;
Height = height;
Position = position;
Club = club;
BirthdayDate = birthdayDate;
IsInvited = isInvited;
IsCaptain = isCaptain;
}
}
}
And here is my Main() part of the program:
static void Main(string[] args)
{
List<PlayerList> players = ReadData();
players = ReadData();
PrintDataToConsole(players);
List<PlayerList> filteredByClub = FilterByClub(players);
PrintFilteredByClub(players, filteredByClub);
List<PlayerList> youngestPlayer = FindYoungestPlayer(players);
Console.ReadKey();
}
The FilterByClub method below, finds all players who are in that particular club and adds it to a new filteredByClub List and the filtered list gets printed on console. I found a similar algorithm somewhere on stackoverflow and applied it to my case to write this method.
private static List<PlayerList> FilterByClub(List<PlayerList> players)
{
return players.FindAll(o => o.Club.Equals("Kauno „Žalgiris“")).ToList();
}
What I'm having troubles with is making another method to filter the youngest player by finding a player with the biggest DateTime value in "class PlayerList" and then adding that player to the new list so then I could pass that new list over to a method that prints it on the console, similarly how I filtered the players with FilterByClub and PrintFilteredByClub methods. There might be a way to return that single youngest player only with making a new List, but I can't even begin to imagine the syntax for that.
I've tried for cycle, foreach, if statements e.t.c, I just can't seem to figure out how to use DateTime value correctly, I keep getting errors that they can't be used as bool or int values and similar mistakes. Is there some special algorithm or syntax that lets you find maximum or minimum value of DateTime from a list of many DateTime values?
Upvotes: 0
Views: 38
Reputation: 5351
private static PlayerList FindYoungest(List<PlayerList> players)
{
return players.OrderByDescending(o => o.BirthdayDate).FirstOrDefault();
}
I hope that helps. It will return a single player (the youngest). Also, I recommend you to change the name of your class to Player instead of PlayerList.
Upvotes: 0