有一个LINQ方法单一项目添加到IEnumerable< T>? [英] Is there a Linq method to add a single item to an IEnumerable<T>?

查看:160
本文介绍了有一个LINQ方法单一项目添加到IEnumerable< T>?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上我试图做这样的事情:

Basically I am trying to do something like this:

image.Layers

它返回一个IEnumerable为除层全层,但在某些情况下,我只想做:

which returns an IEnumerable for all layers except the Parent layer, but in some cases, I just want to do:

image.Layers.With(image.ParentLayer);

,因为它仅用于少数地方相比,通常使用的是由 image.Layers 纳100S。这就是为什么我不想再拍属性也返回了层。

because it's only used in a few places compared to the 100s of the usual usage which is satisfied by image.Layers. That's why I don't want to make another property that also returns the Parent layer.

推荐答案

一种方法是创建一个单独序列出的项目(如数组),然后 Concat的它拖到原来的:

One way would be to create a singleton-sequence out of the item (such as an array), and then Concat it onto the original:

image.Layers.Concat(new[] { image.ParentLayer } )

如果你这样做真的的时候,考虑写一个追加(或类似)扩展法,如一个<一个href="http://stackoverflow.com/questions/3645644/whats-your-favorite-linq-to-objects-operator-which-is-not-built-in/3645715#3645715">listed这里,这将让你做:

If you're doing this really often, consider writing an Append (or similar) extension-method, such as the one listed here, which would let you do:

image.Layers.Append(image.ParentLayer)

这篇关于有一个LINQ方法单一项目添加到IEnumerable&LT; T&GT;?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆