Click here to Skip to main content
15,885,366 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi guys,

i want to convert foreach statement into LINQ in C#...

i have one list that is: CandidateGroups, in this list, i have one more list, that is
canidiatelist.

Here, i'm fetching all candidategroups and then comparing candidategroup.candidatelist.id with current candidiate.ID.

Using 'foreach' i got, but i want to convert this foreach statement to LINQ.

What I have tried:

code

public string GroupNameAR
        {
            get
            {
                string GroupName = "";
                List[CandidateGroupInfo] CandidateGroups = CandidateGroupController.Instance.All;//i have used '[]' instead of '<>', to view code,here.
                foreach(var group in CandidateGroups)
                {
                    foreach (var candidate in group.CandidateList)
                    {
                        if (candidate.ID == ID)
                        {
                            GroupName = group.NameAR;
                        }
                    }
                }
                return GroupName != ""? GroupName.ToUpper() : null;                                               
            }
        }
Posted
Updated 4-Oct-16 23:23pm
v6

C#
int ID = 202;
string GroupName = string.Empty;

List<CandidateGroupInfo> CandidateGroups = new List<CandidateGroupInfo> {
    new CandidateGroupInfo{NameAR = "A", CandidateList = new List<CandidateList> { new CandidateList{ID=101}, new CandidateList{ID=102}}},
    new CandidateGroupInfo{NameAR = "B", CandidateList = new List<CandidateList> { new CandidateList{ID=201}, new CandidateList{ID=202}}},
    new CandidateGroupInfo{NameAR = "C", CandidateList = new List<CandidateList> { new CandidateList{ID=301}, new CandidateList{ID=302}}}
    };

foreach(var group in CandidateGroups)
{
    foreach (var candidate in group.CandidateList)
    {
        if (candidate.ID == ID)
        {
            GroupName = group.NameAR;
            // you should add a break as the data is found so no need
            // to continue processing in the loop
            break;
        }
    }
    // again we want to break the outer loop is the data
    // has been found
    if (!string.IsNullOrWhiteSpace(GroupName))
    {
        break;
    }
}
Console.WriteLine(GroupName != ""? GroupName.ToUpper() : null);

// now via Linq

GroupName = string.Empty;

CandidateGroupInfo info = CandidateGroups.FirstOrDefault(cg => cg.CandidateList.Any(cl => cl.ID == ID));

GroupName = info == null ? string.Empty : info.NameAR.ToUpper();
 
Share this answer
 
Comments
Maciej Los 5-Oct-16 15:42pm    
What about this OP statement: "i want to convert this foreach statement to LINQ"?
F-ES Sitecore 6-Oct-16 4:43am    
The bit of code after "// now via Linq". Thanks for the downvote just because you didn't fully read the solution :)
Maciej Los 6-Oct-16 5:12am    
You have no reason to be sarcastic... 3 stars means neutral vote. I'm far, far away from haste in down-voting.

Now, upvoted!
F-ES Sitecore 6-Oct-16 5:20am    
I've a programmer, it's my job to be sarcastic :D
try
C#
var temp  = CandidateGroups.FirstOrDefault(k => k.CandidateList.Any(a => a.ID == ID));
           GroupName = temp != null ? temp.NameAR.ToUpper() : null;
 
Share this answer
 
v2
Comments
abdul subhan mohammed 5-Oct-16 4:17am    
Error: does not contain a definition for FirstOrDefault
abdul subhan mohammed 5-Oct-16 4:17am    
tried with find, findAll.. also
Karthik_Mahalingam 5-Oct-16 4:19am    
add this
using System.Linq; 
Maciej Los 5-Oct-16 15:40pm    
Looks perfect!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900