如何比较列表中的ID< list< int>>在LINQ [英] How to compare ID in list<list<int>> in LINQ

查看:63
本文介绍了如何比较列表中的ID< list< int>>在LINQ的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我有一个清单,其中还有一个清单。



赞:在候选人中,我有证书列表,在证书中,我有模块列表。



我想比较Candidate.Certificate.Moduels.ID with Given.ModuleID。



我尝试过:



Hi guys,

I have a list and in that, i have one more list.

Like: In Candidate, I have list of Certificates and in Certificate, I have list of Modules.

I want to compare Candidate.Certificate.Moduels.ID with Given.ModuleID.

What I have tried:

List<CandidateCertificateInfo> candidateCertificates = CandidateCertificateController.GetAllCandidateCertificateByCandidateID(candidateSession.CandidateCertificate.CandidateID).Where(item => item.Certification.ProgramID == candidateSession.Module.ProgramID).ToList();

foreach (var candidateCertificate in candidateCertificates)
{
    List<int> candidateCertificateModuleID = candidateCertificate.CandidateCertificateModuleDetails.Select(item => item.ModuleID).ToList();
    foreach (int ccmid in candidateCertificateModuleID)
    {
        if (ccmid == candidateSession.ModuleID)
        {
            //CODE HERE...
        }
    }
}







i想要制作此代码尽可能小。或者是,好吧我写的。



请告诉我。





谢谢




i want to make this code as small as possible. Or is it, ok what i wrote.

Please let me know.


thanks

推荐答案

嗯......将Linq返回值合并到List只是为了将它用作<$ c的目标有点无意义$ c> foreach ...你做了两次。

并且总是有List< T> .ForEach方法,可以完全取代内循环。
Well...it's a bit pointless coalescing the Linq return value to a List just to use it as the target of a foreach ... which you do twice.
And there is always the List<T>.ForEach method, which could replace the inner loop completely.


如果您想使用Linq实现这一点,最合适的方法是 Enumerable.Any [ ^ ]



If you would like to achieve that using Linq, the most appropriate method is Enumerable.Any[^]

var result = candidateCertificates.CandidateCertificateModuleDetails.Any(x=>x.ModuleID == Given.ModuleID);



返回 true 如果找到模块或 false 在其他情况下。


Returns true if module has been found or false in other case.


这篇关于如何比较列表中的ID&lt; list&lt; int&gt;&gt;在LINQ的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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