Question: I have some code for pgp encryption from here:
http://blogs.microsoft.co.il/blogs/kim/archive/2009/01/23/pgp-zip-encrypted-files-with-c.aspx
It has the below method, using some LINQ.
I’m still on .NET 2.0 and can’t switch higher, yet…
How can I replace this expression with ordinary code?
I don’t really understand Linq, I guess it does some sorting ?
private PgpSecretKey GetFirstSecretKey(PgpSecretKeyRingBundle secretKeyRingBundle)
{
foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings())
{
PgpSecretKey key = kRing.GetSecretKeys()
.Cast<PgpSecretKey>()
.Where(k => k.IsSigningKey)
.FirstOrDefault();
if (key != null)
return key;
}
return null;
}
Something like:
foreachimplicitly performs a cast to the target type. Admittedly the original LINQ would have been more pleasantly written as:(You may need to make the first
fromclause strongly typed too; it depends on whatGetKeyRings()is declared to return.)