如何替换 Linq Cast 表达式?
问题:我这里有一些 pgp 加密的代码: http ://blogs.microsoft.co.il/blogs/kim/archive/2009/01/23/pgp-zip-encrypted-files-with-c.aspx
它具有以下方法,使用一些 LINQ。 我仍在 .NET 2.0 上,无法切换到更高版本,但是...
我怎样才能用普通代码替换这个表达式? 我不太了解 Linq,我想它会进行一些排序?
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;
}
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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
类似于:
foreach
隐式执行到目标类型的强制转换。诚然,最初的 LINQ 会更愉快地写成:(您可能也需要将第一个
from
子句设为强类型;这取决于GetKeyRings()
被声明返回。)Something like:
foreach
implicitly 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
from
clause strongly typed too; it depends on whatGetKeyRings()
is declared to return.)