Starting with the next entities definition:
Public Class Certificate{
public ID as int;
}
Public Class Authority{
ID int;
Certificates IEnumerable<Certificate>;
}
My function recives 2 collections: IEnumerable<Certificate>
and IEnumerable<Authority>
. I need to select the Authorities
which Certificates collection
has at least one Certificate in the IEnumerable<Certificate>
input parameter.
My firt implementation is enumerating the IEnumerable<Certificate>
and selecting the Authority
using a Where(predicate)
.
Public IEnumerable<Authority> SelectAuthorities(authList IEnumerable<Authority>, certList IEnumerable<Certificate>){
foreach (Certificate loadedCert in certList) {
yield return auth.Where(a => a.Certificados.Any(c1 => c1.IDCert == loadedCert.IDCert));
}
}
I think there must be a way to avoid the for loop using a more complex linq correlated subquery (I feel it in the "force") but I can't find it.
Can anybody help?
You can use double Any
to avoid loop:
authList.Where(a => a.Certificados
.Any(c1 => certList.Any(c2 => c1.IDCert == c2.IDCert)));
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.