ienumerable

Working of IEnumerator in c#

余生颓废 提交于 2019-12-24 07:16:12
问题 static void Main() { DaysOfTheWeek days = new DaysOfTheWeek(); foreach (string day in days) { Console.Write(day + " "); } // Output: Sun Mon Tue Wed Thu Fri Sat Console.ReadKey(); } public class DaysOfTheWeek : IEnumerable { private string[] days = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat" }; public IEnumerator GetEnumerator() { for (int index = 0; index < days.Length; index++) { // Yield each day of the week. yield return days[index]; } } } What happens in the foreach loop. Does it

Ordered Parallel is not working as expected. (Convert List into IEnumerable?)

ε祈祈猫儿з 提交于 2019-12-24 05:15:07
问题 I have a list of works to do. and i want to run them in parallel in an ordered way. but ordered parallel is trying to split methods into chunks and then execute chunks in ordered way. Here is the reproduced problem. var list = Enumerable.Range(0, 1000).ToList(); list.AsParallel().AsOrdered().ForAll(i => { Task.Delay(1000).Wait(); Console.WriteLine(i); }); I expect this to first print values from start. like or something like this 1 0 2 3 But here is the results. This means ordered parallel is

Find the index of a specific combination without generating all ncr combinations

帅比萌擦擦* 提交于 2019-12-24 02:22:17
问题 I am trying to find the index of a specific combination without generating the actual list of all possible combinations. For ex: 2 number combinations from 1 to 5 produces, 1,2;1,3,1,4,1,5;2,3,2,4,2,5..so..on. Each combination has its own index starting with zero,if my guess is right. I want to find that index without generating the all possible combination for a given combination. I am writing in C# but my code generates all possible combinations on fly. This would be expensive if n and r

accessing C# base class in iterator causes ReSharper warning

自古美人都是妖i 提交于 2019-12-24 01:33:26
问题 I have two classes, GenericList and SpecificList , where SpecificList inherits from GenericList . GenericList implements IEnumerable<GenericItem> and SpecificList implements IEnumerable<SpecificItem> . SpecificItem inherits from GenericItem . I have to implement GetEnumerator in both GenericList and SpecificList since they implement IEnumerable<T> . In GenericList , it's easy enough, I just return the enumerator for the underlying List<T> : public IEnumerator<GenericItem> GetEnumerator() {

Serialization on classes that implement IEnumerator

≯℡__Kan透↙ 提交于 2019-12-24 00:59:59
问题 I have written a program which will serialize and de-serialize, it does this fine (I plan on implementing it on the subclasses once I get it properly working on one). However I have run into trouble when I decided I wanted to be able to iterate through the results using a Foreach . After this didn't work I to find out had to implement the IEnumerator and IEnumerable interfaces and add the required methods to my class. So I have, and this does allow me to loop through my collections. The

Implicit conversion of collections

扶醉桌前 提交于 2019-12-23 22:45:04
问题 If I define an explicit conversion operator between two types, shouldn't it follow that I can explicitly convert between collections of those types? Ie. public static explicit operator FooEntity(Entity entity) { FooEntity e = new FooEntity(entity); return e; } And thus I could do this, IEnumerable<Entity> entities = GetEntities(); IEnumerable<FooEntity> fooEntities = (IEnumerable<FooEntity>)entities; or IEnumerable<FooEntity> fooEntities = entities as IEnumerable<FooEntity> Is this possible

Strange behaviour of OrderBy Linq

耗尽温柔 提交于 2019-12-23 22:26:38
问题 I have a list which is ordered using the OrderBy() Linq function, that returns an IOrderedEnumerable . var testList = myList.OrderBy(obj => obj.ParamName); The ParamName is an object that can hold integer as well as string. The above orderBy orders the list based on the integer value. Now I am operating a foreach on the testList and changing the ParamName property to some string based on its integer value as follows, using (var sequenceEnum = testList.GetEnumerator()) { while (sequenceEnum

How can I convert IEnumerable<object> to List<IFoo> where each object is an IFoo?

亡梦爱人 提交于 2019-12-23 19:51:56
问题 How can I convert IEnumerable<object> to List<IFoo> where each object is an IFoo ? I have an IEnumerable<object> , someCollection , and each item in someCollection is an IFoo instance. How can I convert someCollection into a List<IFoo> ? Can I use convert or cast or something instead of looping through and building up a list? 回答1: Using LINQ, you can use Cast to cast the items, and use ToList to get a list. Try: IEnumerable<object> someCollection; //Some enumerable of object. var list =

How can I randomly ordering an IEnumerable<>?

本秂侑毒 提交于 2019-12-23 17:27:34
问题 I have this IEnumerable : IEnumerable<MyObject> and I need to order the list of the MyObject randomly. Do I need to cast into an ArrayList? Or I can do it directly? Thanks EDIT this is my actual random order function : IList<ArchiePacchettoOfferta> list = new List<ArchiePacchettoOfferta>(m_oEnum); for (int i = 0; i < list.Count; ++i) { HttpContext.Current.Response.Write(list[i].Titolo + "<br />"); } Random rnd = new Random(); for (int i = 0; i < list.Count; ++i) { int swapIndex = rnd.Next(i +

MVC Error: model item of type 'System.Collections.Generic.IEnumerable`1 required

时间秒杀一切 提交于 2019-12-23 13:34:11
问题 I'm doing some modifications to a system, and I've run into an exception that I was hoping someone could help with?? The error I'm getting is the following: Exception Details: System.InvalidOperationException: The model item passed into the dictionary is of type 'System.Collections.Generic.List 1[System.String]', but this dictionary requires a model item of type 'System.Collections.Generic.IEnumerable 1[MyApp.Data.aspnet_Users]'. Basically what I want to do is display a list of Inactive users