Remove items from IEnumerable

I don’t see how the first version would compile, and the second version won’t do anything unless you use the result. It doesn’t remove anything from the existing collection – indeed, there may not even be an in-memory collection backing it. It just returns a sequence which, when iterated over, will return the appropriate values. … Read more

Why doesn’t the Controls collection provide all of the IEnumerable methods?

No, IEnumerable doesn’t have many extension methods on it: IEnumerable<T> does. They are two separate interfaces, although IEnumerable<T> extends IEnumerable. The normal LINQ ways of converting are to use the Cast<T>() and OfType<T>() extension methods which do extend the nongeneric interface: IEnumerable<TextBox> textBoxes = Controls.OfType<TextBox>(); IEnumerable<Control> controls = Controls.Cast<Control>(); The difference between the two is … Read more

Optimal LINQ query to get a random sub collection – Shuffle

Further to mquander’s answer and Dan Blanchard’s comment, here’s a LINQ-friendly extension method that performs a Fisher-Yates-Durstenfeld shuffle: // take n random items from yourCollection var randomItems = yourCollection.Shuffle().Take(n); // … public static class EnumerableExtensions { public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source) { return source.Shuffle(new Random()); } public static IEnumerable<T> Shuffle<T>(this IEnumerable<T> source, Random rng) … Read more

Is there a standard C++ equivalent of IEnumerable in C#?

It isn’t needed in C++, and here’s why: C# only supports dynamic polymorphism. So to create a reusable algorithm, you need an interface which all iterators will implement. That’s IEnumerator<T>, and IEnumerable<T> is a factory for returning an iterator. C++ templates, on the other hand, support duck typing. That means you don’t need to constrain … Read more

With Entity Framework is it better to use .First() or .Take(1) for “TOP 1”?

From LINQPad: C#: age_Centers.Select(c => c.Id).First(); age_Centers.Select(c => c.Id).FirstOrDefault(); age_Centers.Select(c => c.Id).Take(1).Dump(); SQL: SELECT TOP (1) [t0].[Id] FROM [age_Centers] AS [t0] GO SELECT TOP (1) [t0].[Id] FROM [age_Centers] AS [t0] GO SELECT TOP (1) [t0].[Id] FROM [age_Centers] AS [t0] *Note that Take(1) enumerates and returns an IQueryable.

Why do arrays in .net only implement IEnumerable and not IEnumerable?

Arrays do implement IEnumerable<T>, but it is done as part of the special knowledge the CLI has for arrays. This works as if it were an explicit implementation (but isn’t: it is done at runtime). Many tools will not show this implementation, this is described in the Remarks section of the Array class overview. You … Read more

C# Distinct on IEnumerable with custom IEqualityComparer

The problem is with your GetHashCode. You should alter it to return the hash code of AllianceName instead. int IEqualityComparer<Village>.GetHashCode(Village obj) { return obj.AllianceName.GetHashCode(); } The thing is, if Equals returns true, the objects should have the same hash code which is not the case for different Village objects with same AllianceName. Since Distinct works … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)