草庐IT

ienumerable

全部标签

c# - 从 IEnumerable<T> 获取类型 T

有没有办法检索类型T来自IEnumerable通过反射(reflection)?例如我有一个变量IEnumerable信息;我想通过反射获取Child的类型 最佳答案 IEnumerablemyEnumerable;Typetype=myEnumerable.GetType().GetGenericArguments()[0];因此,IEnumerablestrings=newList();Console.WriteLine(strings.GetType().GetGenericArguments()[0]);打印System.S

c# - 具有键 'XXX' 的 ViewData 项的类型为 'System.Int32' 但必须为 'IEnumerable<SelectListItem>' 类型

我有以下View模型publicclassProjectVM{....[Display(Name="Category")][Required(ErrorMessage="Pleaseselectacategory")]publicintCategoryID{get;set;}publicIEnumerableCategoryList{get;set;}....}和以下Controller方法来创建一个新项目并分配一个类别publicActionResultCreate(){ProjectVMmodel=newProjectVM{CategoryList=newSelectList(db

c# - 使用 Enumerable.Empty<T>() 而不是 new List<T>() 来初始化 IEnumerable<T> 是否更好?

假设你有一个类Person:publicclassPerson{publicstringName{get;set;}publicIEnumerableRoles{get;set;}}我显然应该在构造函数中实例化角色。现在,我曾经用这样的列表来做:publicPerson(){Roles=newList();}但是我在System.Linq命名空间中发现了这个静态方法IEnumerableEnumerable.Empty();来自MSDN:TheEmpty(TResult)()methodcachesanemptysequenceoftypeTResult.Whentheobjectit

c# - 从 IEnumerable<KeyValuePair<>> 重新创建字典

我有一个返回IEnumerable>的方法,但一些调用者要求方法的结果是字典。如何转换IEnumerable>进入Dictionary这样我就可以使用TryGetValue?方法:publicIEnumerable>GetComponents(){//...yieldreturnnewKeyValuePair(t.Name,controlInformation);}来电者:Dictionaryactual=target.GetComponents();actual.ContainsKey("something"); 最佳答案 如果您

c# - 如何将两个 IEnumerable<T> 连接成一个新的 IEnumerable<T>?

我有两个IEnumerable的实例(与相同的T)。我想要一个IEnumerable的新实例这是两者的串联。.NET中是否有内置方法可以执行此操作,还是我必须自己编写? 最佳答案 是的,LINQtoObjects支持Enumerable.Concat:vartogether=first.Concat(second);注意:如果first或second为null,您将收到ArgumentNullException。为避免这种情况并像对待空集一样对待空值,请像这样使用空值合并运算符:vartogether=(first??Enumera

c# - 为什么要使用 yield 关键字,而我只能使用普通的 IEnumerable?

给定这段代码:IEnumerableFilteredList(){foreach(objectiteminFullList){if(IsItemInPartialList(item))yieldreturnitem;}}为什么我不应该这样编码?:IEnumerableFilteredList(){varlist=newList();foreach(objectiteminFullList){if(IsItemInPartialList(item))list.Add(item);}returnlist;}我有点理解yield关键字的作用。它告诉编译器构建某种东西(迭代器)。但是为什么要用

c# - 使用 yield return 的 IEnumerable 和递归

我有一个IEnumerable我用来在WebForms页面中查找控件的方法。该方法是递归的,当yieldreturn返回我想要的类型时,我遇到了一些问题。返回递归调用的值。我的代码如下所示:publicstaticIEnumerableGetDeepControlsByType(thisControlcontrol){foreach(Controlcincontrol.Controls){if(cisT){yieldreturnc;}if(c.Controls.Count>0){yieldreturnc.GetDeepControlsByType();}}}这目前会引发“无法转换表达式

c# - 在不迭代的情况下计算 IEnumerable<T> 中的项目?

privateIEnumerableTables{get{yieldreturn"Foo";yieldreturn"Bar";}}假设我想对这些进行迭代并编写类似处理#nof#m的内容。有没有一种方法可以在我的主迭代之前无需迭代就可以找出m的值?我希望我说清楚了。 最佳答案 IEnumerable不支持这个。这是设计使然。IEnumerable使用惰性评估在您需要之前获取您要求的元素。如果你想知道项目的数量而不用遍历它们,你可以使用ICollection,它有一个Count属性(property)。

c# - 如何返回一个空的 IEnumerable?

给出以下代码和给出的建议inthisquestion,我决定修改这个原始方法并询问IEnumarable中是否有任何值返回它,如果没有返回一个没有值的IEnumerable。方法如下:publicIEnumerableFindFriends(){//ManythankstoRex-Mforhishelpwiththisone.//https://stackoverflow.com/users/67/rex-mreturndoc.Descendants("user").Select(user=>newFriend{ID=user.Element("id").Value,Name=user

c# - 为什么在多对多/一对多关系上使用 ICollection 而不是 IEnumerable 或 List<T>?

我在教程中经常看到这种情况,导航属性为ICollection.这是EntityFramework的强制要求吗?我可以使用IEnumerable吗??使用ICollection的主要目的是什么?而不是IEnumerable甚至List? 最佳答案 通常,您选择的内容取决于您需要访问哪些方法。一般-IEnumerable(MSDN:http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx)对于只需要迭代的对象列表,ICollection(MSD