如何替换 Linq Cast 表达式?

发布于 2024-09-28 18:12:07 字数 879 浏览 0 评论 0原文

问题:我这里有一些 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无声情话 2024-10-05 18:12:07

类似于:

foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings())
{
    foreach (PgpSecretKey key in kRing.GetSecretKeys())
    {
        if (key.IsSigningKey)
        {
            return key;
        }
    }
}
return null;

foreach隐式执行到目标类型的强制转换。诚然,最初的 LINQ 会更愉快地写成:(

return (from keyring in secretKeyRingBundle.GetKeyRings()
        from PgpSecretKey key in keyring.GetSecretKeys()
        where key.IsSigningKey)
       .FirstOrDefault(); 

可能也需要将第一个 from 子句设为强类型;这取决于 GetKeyRings() 被声明返回。)

Something like:

foreach (PgpSecretKeyRing kRing in secretKeyRingBundle.GetKeyRings())
{
    foreach (PgpSecretKey key in kRing.GetSecretKeys())
    {
        if (key.IsSigningKey)
        {
            return key;
        }
    }
}
return null;

foreach implicitly performs a cast to the target type. Admittedly the original LINQ would have been more pleasantly written as:

return (from keyring in secretKeyRingBundle.GetKeyRings()
        from PgpSecretKey key in keyring.GetSecretKeys()
        where key.IsSigningKey)
       .FirstOrDefault(); 

(You may need to make the first from clause strongly typed too; it depends on what GetKeyRings() is declared to return.)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文