How can I add an IEnumerable<T> to an existing ICollection<T> - c#

Given an existing ICollection<T> instance (e.g. dest) what is the most efficient and readable way to add items from an IEnumerable<T>?
In my use case, I have some kind of utility method Collect(IEnumerable items) which returns a new ICollection with the elements from items, so I am doing it in the following way:
public static ICollection<T> Collect<T>(IEnumerable<T> items) where T:ICollection<T>
{
...
ICollection<T> dest = Activator.CreateInstance<T>();
items.Aggregate(dest, (acc, item) => { acc.Add(item); return acc; });
...
return dest;
}
Question: Is there any “better” way (more efficient or readable) of doing it?
UPDATE: I think the use of Aggregate() is quite fluent and not so inefficient as invoking ToList().ForEach(). But it does not look very readable. Since nobody else agrees with the use of Aggregate() I would like to read your reasons to NOT use Aggregate() for this purpose.

Just use Enumerable.Concat:
IEnumerable<YourType> result = dest.Concat(items);
If you want a List<T> as result use ToList:
List<YourType> result = dest.Concat(items).ToList();
// perhaps:
dest = result;
If dest is actually already a list and you want to modify it use AddRange:
dest.AddRange(items);
Update:
if you have to add items to a ICollection<T> method argument you could use this extension:
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> seq)
{
List<T> list = collection as List<T>;
if (list != null)
list.AddRange(seq);
else
{
foreach (T item in seq)
collection.Add(item);
}
}
// ...
public static void Foo<T>(ICollection<T> dest)
{
IEnumerable<T> items = ...
dest.AddRange(items);
}

Personally I'd go with #ckruczek's comment of a foreach loop:
foreach (var item in items)
dest.Add(item);
Simple, clean, and pretty much everybody immediately understands what it does.
If you do insist on some method call hiding the loop, then some people define a custom ForEach extension method for IEnumerable<T>, similar to what's defined for List<T>. The implementation is trivial:
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action) {
if (source == null) throw new ArgumentNullException(nameof(source));
if (action == null) throw new ArgumentNullException(nameof(action));
foreach (item in source)
action(item);
}
Given that, you would be able to write
items.ForEach(dest.Add);
I don't see much benefit in it myself, but no drawbacks either.

We actually wrote an extension method for this (along with a bunch of other ICollection extension methods):
public static class CollectionExt
{
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> source)
{
Contract.Requires(collection != null);
Contract.Requires(source != null);
foreach (T item in source)
{
collection.Add(item);
}
}
}
So we can just use AddRange() on an ICollection():
ICollection<int> test = new List<int>();
test.AddRange(new [] {1, 2, 3});
Note: If you wanted to use List<T>.AddRange() if the underlying collection was of type List<T> you could implement the extension method like so:
public static void AddRange<T>(this ICollection<T> collection, IEnumerable<T> source)
{
var asList = collection as List<T>;
if (asList != null)
{
asList.AddRange(source);
}
else
{
foreach (T item in source)
{
collection.Add(item);
}
}
}

Most efficient:
foreach(T item in itens) dest.Add(item)
Most readable (BUT inefficient because it is creating a throwaway list):
items.ToList().ForEach(dest.Add);
Less readable, but Not so inefficient:
items.Aggregate(dest, (acc, item) => { acc.Add(item); return acc; });

items.ToList().ForEach(dest.Add);
If you dont want to create a new collection instance, then create an extension method.
public static class Extension
{
public static void AddRange<T>(this ICollection<T> source, IEnumerable<T> items)
{
if (items == null)
{
return;
}
foreach (T item in items)
{
source.Add(item);
}
}
}
Then you can edit your code like this:
ICollection<T> dest = ...;
IEnumerable<T> items = ...;
dest.AddRange(items);

Related

Can I have a different Extension method for IEnumerable<T> than for <T>?

I have an extension method that works on any class, but I want to call a special version if I am working on IEnumerable<T>.
For Example
public static class ExtensionMethods
{
public static dynamic Test<T>(this T source)
{
dynamic expandoObject = new System.Dynamic.ExpandoObject();
var dictionary = (IDictionary<string,object>)expandoObject;
dictionary["Test"] = source.ToString();
return dictionary;
}
public static IEnumerable<dynamic> Test<T>(this List<T> source)
{
var result = new List<dynamic>();
foreach(var r in source)
yield return r.Test();
}
public static IEnumerable<dynamic> Test<T>(this IEnumerable<T> source)
{
var result = new List<dynamic>();
foreach(var r in source)
yield return r.Test();
}
}
// Usage
public class X
{
string guid = Guid.NewGuid().ToString();
}
void Main()
{
List<X> list = new List<X>() { new X() };
list.Test().Dump(); // Correct but only works because there is an explicit overload for List<T>
var array = list.ToArray();
((IEnumerable<X>) array).Test().Dump(); // Correct
array.Test().Dump(); // Calls the wrong extension method
}
Is there any way I can get array.Test() to call the IEnumerable version without having to explicitly cast it?
Alternatively, if I give the extension method different names, if there any way I can get a compiler error if I accidently use the wrong one?
I think you are trying to solve it in a wrong direction. The List implements IEnumerable interface and as such the compiler can have problem with solving the best method will be invoked on List. What you could do -- you could test if the IEnumerable is a list inside the extension method.
public static IEnumerable<dynamic> Test<T>(this IEnumerable<T> source)
{
if (source is List<T>) {
// here
}
var result = new List<dynamic>();
foreach(var r in source)
yield return r.Test();
}
You can specify T and not rely on type inference, this will hint compiler to use correct extension method. Code would look like this:
var array = list.ToArray();
array.Test<X>().Dump();
What happens is, that compiler cannot tell which extension to use, since Array is valid argument for both method signatures:
public static dynamic Test<T>(this T source) { .. }
public static IEnumerable<dynamic> Test<T>(this IEnumerable<T> source) { .. }
In first case compiler can assume T is of type Array. Because of it, compiler has to picks one (might be first defined?).
Add this extension method to explicitly catch all array types:
public static IEnumerable<dynamic> Test<T>(this T[] source)
{
var result = new List<dynamic>();
foreach(var r in source)
yield return r.Test();
}

LINQ where doesn't accept 1 or 2 parameters

The following code with a boolean parameter works pretty well:
public List<T> SearchByStatus(bool status, List<T> list)
{
return (List<T>)list.Where(_item => _item.Executed == status);
}
But if I want to use something like this
public List<T> SearchByCodeType(ECodes codeType, List<T> list)
{
return (List<T>)list.Where(_item => _item.CodeType == codeType);
}
, the IDE throws an error saying Func<T, int, bool> doesn't accept 1 parameter.
I researched a bit and found for example this.
If I now add a seond parameter, lets say
public List<T> SearchByCodeType(ECodes codeType, List<T> list)
{
return (List<T>)list.Where((_item, _index) => _item.CodeType == codeType);
}
it says Func<T, bool> doens't accept 2 parameters.
The messages itself are correct, but I don't get why it assumes I want to use the overloaded version of Where in the first case and the non-overloaded in the second... Am I doing something wrong?
P.S.: The ECodes-type used is defined as
public enum ECodes : int
{
....
}
May that cause the issue?
Both of these should work fine:
public List<T> SearchByCodeType(ECodes codeType, List<T> list)
{
return list.Where((_item, _index) => _item.CodeType == codeType).ToList();
}
public List<T> SearchByCodeType(ECodes codeType, List<T> list)
{
return list.Where(_item => _item.CodeType == codeType).ToList();
}
If they don't - please check whether you have using System.Linq; at the top, and are using regular LINQ (not something obscure like LINQBridge).
You could also use:
public List<T> SearchByCodeType(ECodes codeType, List<T> list)
{
return list.FindAll(_item => _item.CodeType == codeType);
}
Note that all of this assumes that you have a suitable generic constraint on T such that T.CodeType is well-defined - presumably:
class Foo<T> where T : IHazCodeType
{
List<T> SearchByCodeType(ECodes codeType, List<T> list) {...}
}
interface IHazCodeType
{
ECodes CodeType {get;}
}

linq how to process each element?

I can never remember. How do i process each element in a string? I want to write
stringblah.Split('/n', Split('\n', StringSplitOptions.RemoveEmptyEntries))
.Each(s=>s.Trim());
Are you looking for Select?
var items = stringblah.Split(new[] {'\n'}, StringSplitOptions.RemoveEmptyEntries)
.Select(s => s.Trim());
// ...
foreach (var item in items)
{
Console.WriteLine(item);
}
You can always make your own extension method:
public static class MyExtensions
{
public static void ForEach<T>(this IEnumerable<T> source, Action<T> action)
{
foreach (T element in source) action(element);
}
}
However, I would warn that most people would say you shouldn't do this. Using a traditional foreach loop is considered the better practice.

How does Concat() actually join the collections at lower level?

What is Linq actually doing?
(I'm assuming this is for LINQ to Objects. Anything else will be implemented differently :)
It's just returning everything from the first, and then everything from the second. All data is streamed. Something like this:
public static IEnumerable<T> Concat(this IEnumerable<T> source1,
IEnumerable<T> source2)
{
if (source1 == null)
{
throw new ArgumentNullException("source1");
}
if (source2 == null)
{
throw new ArgumentNullException("source1");
}
return ConcatImpl(source1, source2);
}
private static IEnumerable<T> ConcatImpl(this IEnumerable<T> source1,
IEnumerable<T> source2)
{
foreach (T item in source1)
{
yield return item;
}
foreach (T item in source2)
{
yield return item;
}
}
I've split this into two methods so that the argument validation can be performed eagerly but I can still use an iterator block. (No code within an iterator block is executed until the first call to MoveNext() on the result.)
It enumerates each collection in turn, and yields each element. Something like that :
public static IEnumerable<T> Concat<T>(this IEnumerable<T> source, IEnumerable<T> other)
{
foreach(var item in source) yield return item;
foreach(var item in other) yield return item;
}
(if you look at the actual implementation using Reflector, you will see that the iterator is actually implemented in a separate method)
It depends on the LINQ provider you are using. LinqToSql or L2E might use a database UNION, whereas LINQ to Objects might just enumerate both collections for your in turn.

Append an object to an IEnumerable<> via reflection

I need to be able to access a property via reflection, and, knowing that this property is an IEnumerable, append an object to it.
Something like this:
Object o;
MemberInfo m;
Array arr; // Except use IEnumerable, may have to take account of value/ref types
arr = (Array)((PropertyInfo)m).GetValue(o, null); }
List<o.GetType()> newArr = new List<o.GetType()>(); /* fails */
newArr.AddRange(arr);
newArr.Add(o);
((PropertyInfo)m).SetValue(o, newArr.ToArray(), null);
Can you help me where I'm going wrong :-)
Solution:
See accepted answer comments. Also (Get the actual type of a generic object parameter) is of help.
Once you have an IEnumerable type, use something like this to append to it:
public static IEnumerable<T> Append<T>(this IEnumerable<T> original, T next)
{
foreach (T item in original) yield return item;
yield return next;
}
public static IEnumerable<T> Append<T>(this IEnumerable<T> original, params T[] next)
{
foreach (T item in original) yield return item;
foreach (T item in next) yield return item;
}
public static IEnumerable<T> Append<T>(this IEnumerable<T> original, IEnumerable<T> next)
{
foreach (T item in original) yield return item;
foreach (T item in next) yield return item;
}
It sounds like you're essentially asking how to make a List<T> based on an unknown-at-compile-time type. For this you'll have to use a bit more reflection magic:
Type genericListType = typeof(List<>);
Type listType = genericListType.MakeGenericType(o.GetType());
object listInstance = Activator.CreateInstance(listType);
That would create a List<T> out of a runtime type.
But really, your code would be much simpler if you simply use ArrayList:
ArrayList list = new ArrayList(arr);
list.Add(o);
Array newArray = list.ToArray(o.GetType());

Categories