简体   繁体   English

如何仅为一项创建IEnumerable列表?

[英]How can I create an IEnumerable list for just one item?

I have the following constructor for a class: 我有一个类的以下构造函数:

public SelectList(IEnumerable items);

I have been populating this with the following: 我一直在用以下填充:

        var AccountId = new SelectList(_reference.Get("01")
            .AsEnumerable()
            .OrderBy(o => o.Order), "RowKey", "Value", "00");

Now I would like to populate with one element of data where the data is a list as follows: 现在,我要填充一个数据元素,其中数据是一个列表,如下所示:

RowKey = "", Value = "00"

How can I make an IEnumerable from the above line? 如何从上面的行中使IEnumerable?

Enumerable.Repeat is very convenient way to create iterators returning just one element. Enumerable.Repeat是创建仅返回一个元素的迭代器的一种非常方便的方法。 If you need iterator for 0 items use Enumerable.Empty. 如果您需要0个项目的迭代器,请使用Enumerable.Empty。

Enumerable.Repeat(new {RowKey = "", Value = "00"}, 1);

You could use an anonymous type array: 您可以使用匿名类型数组:

var AccountId = new SelectList(new[] 
                { 
                    new { RowKey = "", Value = "00" }
                }, "RowKey", "Value", "00");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将一个 IEnumerable 异步转换为另一个,就像 LINQ 的 Select() 一样,但对每个转换后的项目使用 await? - How can I asynchronously transform one IEnumerable to another, just like LINQ's Select(), but using await on every transformed item? 如何建立清单 <FileInfo> 来自IEnumerable <string> ? - How can I create a List<FileInfo> from IEnumerable<string>? 如何绑定 ienumerable 列表项 - How do I bind an ienumerable list item 如果我只能定义一个GetEnumerator,为什么还要实现IEnumerable(T)? - Why implement IEnumerable(T) if I can just define ONE GetEnumerator? 如何将项目添加到 IEnumerable&lt;(T item, int? number)&gt;? - How can I add an item to a IEnumerable<(T item, int? number)>? 如何创建 singleton IEnumerable? - How can I create a singleton IEnumerable? 如何将项目添加到 IEnumerable<t> 收藏?</t> - How can I add an item to a IEnumerable<T> collection? 我该如何投射IEnumerable <?> 到IEnumerable <string> ? - How can I cast IEnumerable<?> to IEnumerable<string>? 如何减少IEnumerable <IEnumerable<Foo> &gt;到IEnumerable <Foo> ? - How can I reduce IEnumerable<IEnumerable<Foo>> to IEnumerable<Foo>? 如何在IEnumerable上创建动态Select <T> 在运行时? - How can I create a dynamic Select on an IEnumerable<T> at runtime?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM