如何比较列表中的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只是为了将它用作 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 aforeach
... 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.