Linq“选择”理智问题

发布于 2024-11-03 08:33:01 字数 517 浏览 1 评论 0原文

我的逻辑类似于下面的代码。似乎为了检查这样的空引用,我必须有两个单独的 Select 语句。 linq ninja 能告诉我如何改进这样的查询吗?

List<C> cList = Globals.GetReferences(irrelevantThing) // GetReferences returns List<A>, which are references to B
            .Select(a => a.GetB()) // GetB returns type B, can be null
            .Where(b => b != null && someOtherConditions)
            .Select(c => new C(b)) // C Constructor cannot have a null parameter
            .ToList();

谢谢。

编辑:稍微清理了示例代码。

I have logic similar to the code below. It seems as though in order to check for null references like this, I have to have two separate Select statements. Could a linq ninja tell me in which way a query like this could be improved?

List<C> cList = Globals.GetReferences(irrelevantThing) // GetReferences returns List<A>, which are references to B
            .Select(a => a.GetB()) // GetB returns type B, can be null
            .Where(b => b != null && someOtherConditions)
            .Select(c => new C(b)) // C Constructor cannot have a null parameter
            .ToList();

Thank you.

EDIT: Cleaned up example code a little bit.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

离旧人 2024-11-10 08:33:01

好吧,首先我会更改参数名称 - 在第一个 Select 之后,您会得到一个 B,那么为什么不将其称为 b 呢?

List<B> bList = Globals.GetReferences(irrelevantThing)
            .Select(a => a.GetB()) // GetB returns type B, can be null
            .Where(b => b != null && someOtherConditions)
            .Select(b => new B(b))
            .ToList();

那时,我想知道为什么你有第二个Select...你已经有了一个 B,那么你为什么要创建一个新的呢? B(B old) 构造函数有什么作用?

如果您确实需要其他选择,那对我来说似乎没问题。我的意思是,如果 GetB 很便宜,你总是可以使用:

List<B> bList = Globals.GetReferences(irrelevantThing)
            .Where(a => a.GetB() != null && someOtherConditions)
            .Select(a => new B(a.GetB()))
            .ToList();

...但说实话,我不确定我会不会。

Well, for one thing I'd change the parameter names - after the first Select, you've got a B, so why not call it b?

List<B> bList = Globals.GetReferences(irrelevantThing)
            .Select(a => a.GetB()) // GetB returns type B, can be null
            .Where(b => b != null && someOtherConditions)
            .Select(b => new B(b))
            .ToList();

At that point, I have to wonder why you've got the second Select at all... you've already got a B, so why are you creating a new one? What does the B(B old) constructor do?

If you do need the other select though, that seems okay to me. I mean, if GetB is cheap, you could always use:

List<B> bList = Globals.GetReferences(irrelevantThing)
            .Where(a => a.GetB() != null && someOtherConditions)
            .Select(a => new B(a.GetB()))
            .ToList();

... but I'm not sure I would, to be honest.

姐不稀罕 2024-11-10 08:33:01

我发现使用查询语法和 let 运算符看起来稍微更好一些:

   var queryB = from a in Globals.GetReferences(irrelevantThing)
                let b = a.GetB()
                where b != null && someOtherConditions
                select new B(b);

   var bList = queryB.ToList()

不过取决于您的偏好...

仅供参考,在扩展方法语法中,上面的内容转换为

   var queryB = Globals.GetReferences(irrelevantThing)
            .Select(a => new { a, b = a.GetB() })
            .Where(x => x.b != null && someOtherConditions)
            .Select(x => new B(x.b))
            .ToList();

更新:刚刚意识到您也可以使用select into 子句从字面上翻译确切的原始查询:

   var queryB = from a in Globals.GetReferences(irrelevantThing)
                select a.GetB() into b
                where b != null && someOtherConditions
                select new B(b);

不过仍然更喜欢 let...

I find it slightly better looking with the query syntax and the let operator:

   var queryB = from a in Globals.GetReferences(irrelevantThing)
                let b = a.GetB()
                where b != null && someOtherConditions
                select new B(b);

   var bList = queryB.ToList()

Depends on your preferences though...

FYI in extension method syntax, the above translates to

   var queryB = Globals.GetReferences(irrelevantThing)
            .Select(a => new { a, b = a.GetB() })
            .Where(x => x.b != null && someOtherConditions)
            .Select(x => new B(x.b))
            .ToList();

UPDATE: just realized that you can also use the select into clause to translate the exact original query literally :

   var queryB = from a in Globals.GetReferences(irrelevantThing)
                select a.GetB() into b
                where b != null && someOtherConditions
                select new B(b);

still prefer let though...

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