There is no ViewData item of type ‘IEnumerable’ that has the key ‘xxx’

Ok, so the answer was derived from some other posts about this problem and it is: If your ViewData contains a SelectList with the same name as your DropDownList i.e. “submarket_0”, the Html helper will automatically populate your DropDownList with that data if you don’t specify the 2nd parameter which in this case is the … Read more

Convert IAsyncEnumerable to List

Sure – you just need the ToListAsync() method, which is in the System.Linq.Async NuGet package. Here’s a complete example: Project file: <Project Sdk=”Microsoft.NET.Sdk”> <PropertyGroup> <OutputType>Exe</OutputType> <TargetFramework>netcoreapp3.1</TargetFramework> </PropertyGroup> <ItemGroup> <PackageReference Include=”System.Linq.Async” Version=”4.0.0″ /> </ItemGroup> </Project> Code: using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; class Program { static async Task Main(string[] args) { IAsyncEnumerable<string> sequence = … Read more

Is there a Linq method to add a single item to an IEnumerable?

One way would be to create a singleton-sequence out of the item (such as an array), and then Concat it onto the original: image.Layers.Concat(new[] { image.ParentLayer } ) If you’re doing this really often, consider writing an Append (or similar) extension-method, such as the one listed here, which would let you do: image.Layers.Append(image.ParentLayer) .NET Core … Read more

Cannot apply indexing with [] to an expression of type ‘System.Collections.Generic.IEnumerable

Because it’s not. Indexing is covered by IList. IEnumerable means “I have some of the powers of IList, but not all of them.” Some collections (like a linked list), cannot be indexed in a practical way. But they can be accessed item-by-item. IEnumerable is intended for collections like that. Note that a collection can implement … Read more

Is it possible to do start iterating from an element other than the first using foreach?

Yes. Do the following: Collection<string> myCollection = new Collection<string>; foreach (string curString in myCollection.Skip(3)) //Dostuff Skip is an IEnumerable function that skips however many you specify starting at the current index. On the other hand, if you wanted to use only the first three you would use .Take: foreach (string curString in myCollection.Take(3)) These can … Read more

Does LINQ work with IEnumerable?

You can use Cast<T>() or OfType<T> to get a generic version of an IEnumerable that fully supports LINQ. Eg. IEnumerable objects = …; IEnumerable<string> strings = objects.Cast<string>(); Or if you don’t know what type it contains you can always do: IEnumerable<object> e = objects.Cast<object>(); If your non-generic IEnumerable contains objects of various types and you … Read more