Linq查询C#中的嵌套列表 [英] Linq query for nested list in C#

查看:778
本文介绍了Linq查询C#中的嵌套列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我在这个列表中有一个List< candidate> ;.



,对于每个候选人,我有List< candidatecertificates> ;.



我有一份证书下拉列表。



Hi guys,

I have a List<candidate>.

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

I have a dropdownlist of Certificates.

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:

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想为此写LINQ查询。



任何人都可以帮助我。





谢谢



i would like to write LINQ query for this.

can anyone plz help me.


Thanks

推荐答案

只需更改您要查询的条件即可。我正在将您的FindAll转换为Where子句,因为我知道它适用于嵌套查询。



我也将此转换为方法,因为作为属性存在ddlCertificate具有空值的可能性,如果过早地调用该属性,则会导致异常。只需在selectValue上调用你的解析,然后再将其传递给方法。



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.

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();           
}





这里的关键是(假设你正在运行一个合理的ORM),这将被推下去管道作为单个查询,作为单个结果返回,而不是由应用程序循环,节省一些周期



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


这篇关于Linq查询C#中的嵌套列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆