ienumerable

Enumerable giving unexpected output

class Foo { public static IEnumerable<int> Range(int start, int end) { return Enumerable.Range(start, end); } public static void PrintRange(IEnumerable<int> r) { foreach (var item in r) { Console.Write(" {0} ", item); } Console.WriteLine(); } } class Program { ...

C# Check for missing number in sequence

I have an List<int> which contains 1,2,4,7,9 for example. I have a range from 0 to 10. Is there a way to determine what numbers are missing in that sequence? I thought LINQ might provide an option but I can't see one In the real world my List could contain 100,000 items so performance is key ...

C# Using Enumerable Range and Except with custom class to determine missing sequence number

I have a List<MyClass> The class is like this: private class MyClass { public string Name{ get; set; } public int SequenceNumber { get; set; } } I want to work out what Sequence numbers might be missing. I can see how to do this here however because this is a class I am unsure what to do? ...

Public property List needs to Concat 2 types with inheritance

I have 2 lists: one of type A and one of type Aa. type Aa is inherited from type A. So: List<A> listA = new List<A>(); List<Aa> listAa = new List<Aa>(); with class Aa : A I have: public property Lists<A> { get { List<A> newList = new List<A>(); //return concat of both lists foreach(List l in listA) { ne...

Entity Framework query not returning correctly enumerated results.

I have this really strange problem where my entity framework query isn't enumerating correctly. The SQL Server table I'm using has a table with a Sku field, and the column is "distinct". It isn't a key, but it doesn't contain any duplicate values. Using actual SQL with where, distinct and group by cluases I have confirmed this. Howev...

IEnumerable<CustomType> in PowerShell

I'm trying to use Enumerable.ToList() in PowerShell. Apparently, to do that, I have to explicitly convert the object to IEnumerable<CustomType>, but I am unable to do that. It seems I can't correctly write IEnumerable<CustomType> in PowerShell. Both IEnumerable<string> and CustomType itself work correctly (the custom type I'm trying to u...

Foreach over a collection of IEnumerables

I have 3 (Edit) mutually exclusive IEnumerables that I want to iterate over. I want to do something like this: IEnumerable<Car> redCars = GetRedCars(); IEnumerable<Car> greenCars = GetGreenCars(); IEnumerable<Car> blueCars = GetBlueCars(); foreach(Car c in (redCars + greenCars + blueCars)) { c.DoSomething(); } ... The best way ...

Why does a function that takes IEnumerable<interface> not accept IEnumerable<class>?

Say, for instance, I have a class: public class MyFoo : IMyBar { ... } Then, I would want to use the following code: List<MyFoo> classList = new List<MyFoo>(); classList.Add(new MyFoo(1)); classList.Add(new MyFoo(2)); classList.Add(new MyFoo(3)); List<IMyBar> interfaceList = new List<IMyBar>(classList); But this produces the e...

C# yield in nested method

If I step through the following code the call to ReturnOne() is skipped. static IEnumerable<int> OneThroughFive() { ReturnOne(); yield return 2; yield return 3; yield return 4; yield return 5; } static IEnumerator<int> ReturnOne() { yield return 1; } I can only assume the compiler is stripping it out because ...

Where can I learn the ins and outs of enumerators in C#?

Is there a good resource out there that explains the concept of enumerators and custom enumerators? Particularly one with a good solid example of why you would want to implement IEnumerable yourself and how you would use it effectively? I occasionally come across yield and I am trying to get a better understanding of it. ...

IEnumerable and IEnumerator in the same class, bad idea?

Is this a bad idea? Private Class GH_DataStructureEnumerator(Of Q As Types.IGH_Goo) Implements IEnumerable(Of Q) Implements IEnumerator(Of Q) .... .... 'Current, MoveNext, Reset etc.' .... .... Public Function GetEnumerator_Generic() As IEnumerator(Of Q) _ Implements IEnumerable(Of Q).GetEnumerator ...

Difference between IEnumerable Count() and Length

What are the key differences between IEnumerable Count() and Length? ...

C# Custom Dictionary Take - Convert Back From IEnumerable

Scenario Having already read a post on this on the same site, which didn't work, I'm feeling a bit stumped but I'm sure I've done this before. I have a Dictionary. I want to take the first 200 values from the Dictionary. CODE Dictionary<int,SomeObject> oldDict = new Dictionary<int,SomeObject>(); //oldDict gets populated somewhere...

IEnumerable<T>.Concat -- A replacement that can work without changing the reference?

Hello, I've recently been bitten by the (way too commmon in my opinion) gotcha of Concat returns it's result, rather than appending to the list itself. For instance. List<Control> mylist=new List<Control>; //.... after adding Controls into mylist MyPanel.Controls.Concat(mylist); //This will not affect MyPanel.Controls at all. MyPane...

Returning Index in Enumerable Select

I have a List<MyClass> with 2 items which have a SequenceNumber property. If I use this code below the returned index is 0 not 1: var test = TrackingCollection .Where(x => x.SequenceNumber == 2) .Select((item, index) => new { ...

Tell LINQ Distinct which item to return

I understand how to do a Distinct() on a IEnumerable and that I have to create an IEqualityComparer for more advanced stuff however is there a way in which you can tell which duplicated item to return? For example say you have a List<T> List<MyClass> test = new List<MyClass>(); test.Add(new MyClass {ID = 1, InnerID = 4}); test.Add(new ...

How to enable Automatic Sorting of IEnumerable Data in GridView?

How can I enable automatic sorting of my BLL which returns a list, CustomerList:List in a GridView? Customer is my own strongly typed class and CustomerList is a List of customers. I know one approach is to set the AllowSorting property to true in the GridView and handle the OnSorting event and calling a sorting method defined in my C...

IEnumerable when I get a real reference on the objects?

IEnumerable<T> collection; void MyMethod(T toSearch) { foreach (T t in collection) if (t == toSearch) {} } The if clause is never true.. is it because the Enumerator creates all the items instances on demand, so everytime a new Reference ? Edited: Another question. What happens if I one of those collection elements return in T...

Automapper Type Converter from String to IEnumerable<String> is not being called

Here is my custom Type Converter. public class StringListTypeConverter : TypeConverter<String, IEnumerable<String>> { protected override IEnumerable<string> ConvertCore(String source) { if (source == null) yield break; foreach (var item in source.Split(',')) yield return item.Trim(); ...

Equality between two enumerables

I have two enumerables with the exact same reference elements, and wondering why Equals wouldn't be true. As a side question, the code below to compare each element works, but there must be a more elegant way Cheers, Berryl var other = (ActivityService) obj; if (!AllAccounts.Count().Equals(other.AllAccounts.Count())) return false; for...