Concatenate multiple IEnumerable
Use SelectMany: public static IEnumerable<T> Concatenate<T>(params IEnumerable<T>[] lists) { return lists.SelectMany(x => x); }
Use SelectMany: public static IEnumerable<T> Concatenate<T>(params IEnumerable<T>[] lists) { return lists.SelectMany(x => x); }
The Except method returns IEnumerable, you need to convert the result to list: list1 = list1.Except(list2).ToList(); Here’s a complete example: List<String> origItems = new List<String>(); origItems.Add(“abc”); origItems.Add(“def”); origItems.Add(“ghi”); List<String> newItems = new List<String>(); newItems.Add(“abc”); newItems.Add(“def”); newItems.Add(“super”); newItems.Add(“extra”); List<String> itemsOnlyInNew = newItems.Except(origItems).ToList(); foreach (String s in itemsOnlyInNew){ Console.WriteLine(s); } Since the only items which don’t exist … Read more
This is really a two part question. 1) Is there inherently anything wrong with returning an IEnumerable<T> No nothing at all. In fact if you are using C# iterators this is the expected behavior. Converting it to a List<T> or another collection class pre-emptively is not a good idea. Doing so is making an assumption … Read more
What exactly do you mean by thread-safe? You certainly shouldn’t change the dictionary while you’re iterating over it, whether in the same thread or not. If the dictionary is being accessed in multiple threads in general, the caller should take out a lock (the same one covering all accesses) so that they can lock for … Read more
As noted in the comments, add using System.Data.Entity (under the Entity Framework package) to get the QueryableExtensions. For .NET Core, these methods are under Microsoft.EntityFrameworkCore
Have you tried to use the Map method that way: Mapper.Map<DestinationClass, SourceClass>(object to convert) ? With the 2.2 version of AutoMapper, this is how we use it and it works fine for us.
any of: values = new Dictionary<string,string> { {“Name”, “John”}, {“City”, “NY”} }; or values = new [] { new KeyValuePair<string,string>(“Name”,”John”), new KeyValuePair<string,string>(“City”,”NY”) }; or: values = (new[] { new {Key = “Name”, Value = “John”}, new {Key = “City”, Value = “NY”} }).ToDictionary(x => x.Key, x => x.Value);
Yes, it is. You can find out a lot more about it in chapter 6 of my book, C# in Depth. Fortunately chapter 6 is available for free from Manning’s web site. I also have two other articles on the book’s web site. Feedback welcome.
Nope, that’s about as concise as you’ll get using built-in language/framework features. You could always create an extension method if you prefer: arr = arr.Append(“JKL”); // or arr = arr.Append(“123”, “456”); // or arr = arr.Append(“MNO”, “PQR”, “STU”, “VWY”, “etc”, “…”); // … public static class EnumerableExtensions { public static IEnumerable<T> Append<T>( this IEnumerable<T> source, … Read more
Well, something will have to loop… but in LINQ you could easily use the Concat and ToList extension methods: var bigList = list1.Concat(list2).Concat(list3).ToList(); Note that this will create a new list rather than appending items to an existing list. If you want to add them to an existing list, List<T>.AddRange is probably what you’re after: … Read more