The order of elements in Dictionary

The order of elements in a dictionary is non-deterministic. The notion of order simply is not defined for hashtables. So don’t rely on enumerating in the same order as elements were added to the dictionary. That’s not guaranteed. Quote from the doc: For purposes of enumeration, each item in the dictionary is treated as a … Read more

Check if one IEnumerable contains all elements of another IEnumerable

There is no “fast way” to do this unless you track and maintain some state that determines whether all values in one collection are contained in another. If you only have IEnumerable<T> to work against, I would use Intersect. var allOfList1IsInList2 = list1.Intersect(list2).Count() == list1.Count(); The performance of this should be very reasonable, since Intersect() … Read more

ReadOnlyCollection or IEnumerable for exposing member collections?

More modern solution Unless you need the internal collection to be mutable, you could use the System.Collections.Immutable package, change your field type to be an immutable collection, and then expose that directly – assuming Foo itself is immutable, of course. Updated answer to address the question more directly Is there any reason to expose an … Read more

ICollection Vs List in Entity Framework

Entity Framework would use ICollection<T> because it needs to support Add operations, which are not part of the IEnumerable<T> interface. Also note that you were using ICollection<T>, you were merely exposing it as the List<T> implementation. List<T> brings along with it IList<T>, ICollection<T>, and IEnumerable<T>. As for your change, exposing via the interface is a … Read more

What’s the difference between IQueryable and IEnumerable [duplicate]

IEnumerable<T> represents a forward-only cursor of T. .NET 3.5 added extension methods that included the LINQ standard query operators like Where and First, with any operators that require predicates or anonymous functions taking Func<T>. IQueryable<T> implements the same LINQ standard query operators, but accepts Expression<Func<T>> for predicates and anonymous functions. Expression<T> is a compiled expression … Read more

How to get the index of an element in an IEnumerable?

I’d question the wisdom, but perhaps: source.TakeWhile(x => x != value).Count(); (using EqualityComparer<T>.Default to emulate != if needed) – but you need to watch to return -1 if not found… so perhaps just do it the long way public static int IndexOf<T>(this IEnumerable<T> source, T value) { int index = 0; var comparer = EqualityComparer<T>.Default; … Read more

IEnumerable to string [duplicate]

You can use String.Concat(). var allowedString = String.Concat( inputString.Where(c => allowedChars.Contains(c)) ); Caveat: This approach will have some performance implications. String.Concat doesn’t special case collections of characters so it performs as if every character was converted to a string then concatenated as mentioned in the documentation (and it actually does). Sure this gives you a … Read more

Return all enumerables with yield return at once; without looping through

It is something that F# supports with yield! for a whole collection vs yield for a single item. (That can be very useful in terms of tail recursion…) Unfortunately it’s not supported in C#. However, if you have several methods each returning an IEnumerable<ErrorInfo>, you can use Enumerable.Concat to make your code simpler: private static … Read more