Hi,
I am using a class object (clsDetails) to store the player details as shown below. I am trying to filter a list of Class objects with same values using LINQ.
I want to get the list of Players who are successful in all the 5 attempts. I am trying to get the do it using LINQ but it does not return the correct players.
I also want to get
1. List of players who are successful in the first attempt itself
2. List of player who are successful in first or second attempt
3. List of players who are successful in at least 3 attempts.
4. List of players who are successful in all the 5 attempts
I am not sure what is the issue with the below query and why it does not return any value.
Thanks
Ashok
I am using a class object (clsDetails) to store the player details as shown below. I am trying to filter a list of Class objects with same values using LINQ.
I want to get the list of Players who are successful in all the 5 attempts. I am trying to get the do it using LINQ but it does not return the correct players.
class Program { static void Main(string[] args) { List<clsDetails> lstAllDetails = new List<clsDetails>(); try { clsDetails cd; //Player 1 cd = new clsDetails() { iAttemptId =1, sPlayerId="P1",sPlayerSuccess="YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 2, sPlayerId = "P1", sPlayerSuccess = "NO" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 3, sPlayerId = "P1", sPlayerSuccess = "NO" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 4, sPlayerId = "P1", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 5, sPlayerId = "P1", sPlayerSuccess = "NO" }; lstAllDetails.Add(cd); //Player 2 cd = new clsDetails() { iAttemptId = 1, sPlayerId = "P2", sPlayerSuccess = "NO" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 2, sPlayerId = "P2", sPlayerSuccess = "NO" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 3, sPlayerId = "P2", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 4, sPlayerId = "P2", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 5, sPlayerId = "P2", sPlayerSuccess = "NO" }; lstAllDetails.Add(cd); //Player 3 cd = new clsDetails() { iAttemptId = 1, sPlayerId = "P3", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 2, sPlayerId = "P3", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 3, sPlayerId = "P3", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 4, sPlayerId = "P3", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 5, sPlayerId = "P3", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); //Player 4 cd = new clsDetails() { iAttemptId = 1, sPlayerId = "P4", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 2, sPlayerId = "P4", sPlayerSuccess = "NO" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 3, sPlayerId = "P4", sPlayerSuccess = "NO" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 4, sPlayerId = "P4", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 5, sPlayerId = "P4", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); //Player 5 cd = new clsDetails() { iAttemptId = 1, sPlayerId = "P5", sPlayerSuccess = "NO" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 2, sPlayerId = "P5", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 3, sPlayerId = "P5", sPlayerSuccess = "NO" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 4, sPlayerId = "P5", sPlayerSuccess = "NO" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 5, sPlayerId = "P5", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); //Player 6 cd = new clsDetails() { iAttemptId = 1, sPlayerId = "P6", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 2, sPlayerId = "P6", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 3, sPlayerId = "P6", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 4, sPlayerId = "P6", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); cd = new clsDetails() { iAttemptId = 5, sPlayerId = "P6", sPlayerSuccess = "YES" }; lstAllDetails.Add(cd); //players successfull in first attempt itself var vSuccessFirstAttempt = (from d in lstAllDetails where d.sPlayerSuccess.ToUpper() == "YES" && d.iAttemptId == 1 select d.sPlayerId); //Players successfull in all the 5 attempts var vSuccessAllAttempts = (from d in lstAllDetails group d by new {d.sPlayerId,d.sPlayerSuccess} into grp where grp.Count() ==1 && grp.Key.sPlayerSuccess =="YES" select grp.Key.sPlayerId); // vSuccessAllAttempts - Enumeration yielded No result } catch (Exception ex) { } } } public class clsDetails { public int iAttemptId = 0; public string sPlayerId = string.Empty; public string sPlayerSuccess = string.Empty; }
I also want to get
1. List of players who are successful in the first attempt itself
2. List of player who are successful in first or second attempt
3. List of players who are successful in at least 3 attempts.
4. List of players who are successful in all the 5 attempts
I am not sure what is the issue with the below query and why it does not return any value.
var vSuccessAllAttempts = (from d in lstAllDetails group d by new {d.sPlayerId,d.sPlayerSuccess} into grp where grp.Count() ==1 && grp.Key.sPlayerSuccess =="YES" select grp.Key.sPlayerId);
Thanks
Ashok
Last edited by a moderator: