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

I have a List<candidate>.

in this list, For each candidate, i have List<candidatecertificates>.

I have a dropdownlist of Certificates.

C#
I want to compare <candidatecertificate>.ceritifcateID
  with ddlCerifiticate_Selected_value.


I want to fetch all the candidates with particualr certificate ID, only.


What I have tried:

C#
private List<CandidateInfo> Candidates
{
            get
            {
                var _candidates = CandidateController.Instance.All.FindAll(item => item.Center.ID == ExamSession.Center.ID);
                foreach(var c in _candidates)
                {
                    foreach (var ct in c.CandidateCertificates)
                    {
                        var ct1 = ct.CertificationID == long.Parse(ddlCertificate.SelectedValue);
                        break;
                    }
                    return c;
                }
                return null;
            }
        }


i would like to write LINQ query for this.

can anyone plz help me.


Thanks
Posted
Updated 20-Oct-16 2:32am
v2
Comments
F-ES Sitecore 20-Oct-16 5:00am    
Your code as posted makes no logical sense so it will be hard to convert to linq. If your logic isn't working now, converting it to linq isn't going to make it work by magic. This is what you have now as LINQ

var candidate = CandidateController.Instance.All.FindAll(item => item.Center.ID == ExamSession.Center.ID).FirstOrDefault();
abdul subhan mohammed 20-Oct-16 5:48am    
that's why, i need help to make the query sensible.
johannesnestler 20-Oct-16 6:08am    
does your Code work now? why change it? - just don't re-parse the selected value all the time during the loop (do it once outside)

1 solution

Just change the conditions that you are querying on. I'm converting your FindAll to a Where clause as I know that works for nested queries.

I'm also converting this to a method, since as a property the possibility exists that ddlCertificate has a null value, which will cause an exception if the property is called prematurely. Just call your parse on the selectedValue before passing it to the method.

C#
private List<CandidateInfo> Candidates(long selected)
{ 
   return CandidateController.Instance.All
      .Where(item => 
         item.Center.ID == ExamSession.Center.ID &&
         item.CandidateCertificates.Any(cert => cert.CertificationID == selected))
      .ToList();           
}


The key here is that (assuming you're running off a sensible ORM) this will be pushed down the pipe as a single query, returned as a single result, and not looped over by the application, sparing some cycles
 
Share this answer
 

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