ienumerable

How to make an IEnumerable of values of tree nodes?

匆匆过客 提交于 2019-12-12 18:00:48
问题 Consider the following class: class TreeNode { int value; public TreeNode l, r; public TreeNode(int value) { this.value = value; } public IEnumerable<int> sub_values() { if (l != null) foreach (int i in l.sub_values()) yield return i; if (r != null) foreach (int i in r.sub_values()) yield return i; yield return value; } } Each value is passed O(h) times, where h is the height of the tree. First, in yield return value; statement, then in yield return i; of each parent node. So, sub_values

Casting List<MyObject> to IEnumerable<MyInterface>

血红的双手。 提交于 2019-12-12 16:57:11
问题 I'm still learning some of this c# stuff, and I couldn't find an answer to this question. Assuming that I have a list of MyObject implementing MyInterface public class MyObject : IMyInterface { ...} public List<MyObject> MyObjectList; How can I return an IEnumerable<IMyInterface> with the contents of MyObjectList? I mean, right now I have this: List<IMyInterface> temp = new List<IMyInterface>(); foreach (MyObject obj in MyObjects) temp.Add(obj); return (IEnumerable<IMyInterface>)temp; But is

Should a GetEnumerator method still be idempotent when the class does not implement IEnumerable

雨燕双飞 提交于 2019-12-12 16:28:36
问题 This question piggy backs of another question which I raised regarding abusing the IEnumerable interface by modifying an object as you iterate over it. The general consensus is that no anything that Implements IEnumerable should be idempotent. But .net supports compile time duck typing with the foreach statement. Any object that provides an IEnumerator GetEnumerator() method can be used inside a foreach statement. So should the GetEnumerator method be idempotent or is it when it implements

Determine if LINQ Enumerable contains object based on a condition? [duplicate]

血红的双手。 提交于 2019-12-12 13:41:27
问题 This question already has answers here : Linq Contains method for a object (4 answers) Closed 6 years ago . I have an IEnumerable<Project> I want to know if this list has any element Project.ID == someID . Is there a way to do that? 回答1: Yes, you want to use the Any method (documentation). IEnumerable<Project> projects = SomeMethodReturningProjects(); if(projects.Any(p => p.ID == someID)) { //Do something... } 回答2: You can use the Any() extension method. var hasAny = projectList.Any(proj =>

How should I get the length of an IEnumerable? [duplicate]

岁酱吖の 提交于 2019-12-12 12:33:50
问题 This question already has answers here : Count the items from a IEnumerable<T> without iterating? (18 answers) Closed last year . I was writing some code, and went to get the length of an IEnumerable. When I wrote myEnumerable.Count() , to my surprise, it did not compile. After reading Difference between IEnumerable Count() and Length, I realized that it was actually Linq that was giving me the extension method. Using .Length does not compile for me either. I am on an older version of C#, so

How is IEnumerable<T> Contra-variant?

一曲冷凌霜 提交于 2019-12-12 11:22:53
问题 This post (http://blogs.msdn.com/b/brada/archive/2005/01/18/355755.aspx) says IEnumerable<T> is Contra-variant. However type T is co-variant because it is an out parameter. So in what context is IEnumerable<T> Contra-variant ?? Hope I am not confusing! Thanks for the answers in advance! 回答1: IEnumerable isn't contra-variant. It's covariant. From MSDN (IEnumerable<(Of <(T>)>) Interface) we have that: Type Parameters out T The type of objects to enumerate. This type parameter is covariant .

Mapping a grouped collection using AutoMapper

不羁岁月 提交于 2019-12-12 11:05:13
问题 I have the following code which is not working: var groupedZones = this._zoneDataManager.GetZonesGroupedByCountry(); IEnumerable<IGrouping<String, ZoneDTO>> zonesToReturn = Mapper.Map<IEnumerable<IGrouping<String, Zone>>, IEnumerable<IGrouping<String, ZoneDTO>>>(groupedZones); I keep getting the following exception: The value \"System.Collections.Generic.List 1[SCGRE.Business.Model.ZoneDTO]\" is not of type \"System.Linq.IGrouping 2[System.String,SCGRE.Business.Model.ZoneDTO]\" and cannot be

In c# , how to iterate IEnumerable in multithreading environment

此生再无相见时 提交于 2019-12-12 10:45:28
问题 I am in this situation that there is a large dictionary that is randomly updated by one thread at a fairly high frequency, and there is another thread that tries to take a snapshot of the dictionary to save as history. I currently using something like this: Dictionary<string, object> dict = new Dictionary<string, object>(); var items = dict.Values.ToList(); This works fine for most of the time, except it occasionally throws: System.InvalidOperationException: Collection was modified;

Index of current item in mvc display template

此生再无相见时 提交于 2019-12-12 10:44:17
问题 I have an mvc page with a displaytemplate. How do I get the index of the current item being rendered in the displaytemplate. it produces correct bindable results in the name attributes. <input name="xxx[0].FirstName"/> <input name="xxx[1].FirstName"/> I want that index value in the display template. Is it in the ViewContext somewhere? @* page.cshtml @ @model ...@ property Contacts is IEnumerable *@ <table id="contacts" class="editable"> <thead> <tr><th>Name</th><th>Surname</th><th>Contact

why ForEach Linq Extension on List rather than on IEnumerable [duplicate]

Deadly 提交于 2019-12-12 08:27:34
问题 This question already has answers here : Closed 8 years ago . Possible Duplicate: Why is there not a ForEach extension method on the IEnumerable interface? Hello, My question is why Foreach extension method is defined on List rather than IEnumreable. i have read Eric Lippert's article but the point is, if it is so bad to have such method than why is it there for List? 回答1: List<T>.ForEach() isn't an extension method. It's just a method on List<T> . One of the main reasons that it isn't