C# foreach on a collection of an interface - c#

I'm wondering if there is any functionality built in to C#/LINQ to simplify the following:
foreach(var item in collection)
{
if (item.GetType() == typeof(Type1)
DoType1(item as Type1);
else if (item.GetType() == typeof(Type2))
DoType2(item as Type2);
...
}
to something along the lines of:
collection.ForEachType(Type1 item => DoType1(item), Type2 item => DoType2(item));
I realize that the following is close:
collection.OfType<Type1>.ToList().Foreach(item => DoType1(item));
collection.OfType<Type2>.ToList().Foreach(item => DoType2(item));
But it does not work when the code is dependent on the order of the collection.

The first thing I'd look at is polymorphism; can I instead use a virtual method, and item.DoSomething()?
The next thing I'd look at would be an enum discriminator, i.e.
switch(item.ItemType) {
case ItemType.Foo: ...
case ItemType.Bar: ...
}
(and add the discriminator to the common interface/base-class)
If the types could be anything, then 4.0 has a trick; if you call te method the same thing for every overload, you can get dynamic to worry about picking it:
dynamic x = item;
DoSomething(x);

There's nothing built into LINQ, no. I would caution you against using GetType() like this though - usually it's more appropriate to use is or as followed by a null check:
foreach(var item in collection)
{
Type1 itemType1 = item as Type1;
if (itemType1 != null)
{
DoType1(itemType1);
continue;
}
Type2 itemType2 = item as Type1;
if (itemType2 != null)
{
DoType2(itemType1);
continue;
}
// etc
}
That way derived classes will be treated in a way which is usually the appropriate one.
This sort of type testing is generally frowned upon, mind you - it's generally better to put the behaviour into the type itself as a virtual method, and call it polymorphically.

What about something like:
var typeActions = new Dictionary<Type,Action<Object>>();
typeActions.Add(typeof(Type1), obj => DoType1((Type1)obj));
typeActions.Add(typeof(Type2), obj => DoType2((Type2)obj));
collection.Foreach(obj => typeActions[obj.GetType()](obj));
This code is untested (typed directly into the browser).

Your mileage may vary.
Dictionary<Type, Action<object>> typeMap = new Dictionary<Type, Action<object>>();
typeMap[typeof(Type1)] = item => DoType1(item as Type1);
typeMap[typeof(Type2)] = item => DoType2(item as Type2);
var typeToActionQuery =
from item in source
let type = item.GetType()
where typeMap.ContainsKey(type)
select new
{
input = item;
method = typeMap[type]
};
foreach(var x in typeToActionQuery)
{
x.method(x.input);
}
Here's a version of the matching query which considers derived types (Note, an item may be matched to more than 1 type, and therefore handled multiple times).
var typeToActionQuery =
from item in source
from kvp in typeMap
where kvp.Key.IsInstanceOfType(item)
select new
{
input = item;
method = kvp.Value
};

It seems to me that if you just replace "item.GetType() == typeof( Type1 )" with "item is Type1", your foreach loop will be simple enough.

Not by default. Try Reactive Extensions or Elevate
The Reactive Extensions and Elevate both contain a ForEach implementation. Both have quite a few methods that extend the functionality of linq.
You won't find a ForEachType, but ForEach (Rx or Elevate) and OfType<> (Linq) will give you what you want.

Related

Get generic type from an instance

I have a list that contains objects of type IMyClass.
This list contains instances of typed class MyClass<`T>.
I need to take two random objects from the list of the same MyClass type and perform an operation on them.
My code:
var item1 = list[random.Next(list.Count)];
...
var subset = list.OfType<MyClass<item1_T>>().ToList();
var item2 = subset[random.Next(subset.Count)];
What do I have to fill in at the dots to get the type item1_T?
Unfortunately in C# it is no possible to dynamically specify a generic type using the angle brackets notation as you wrote:
var subset = list.OfType<MyClass<item1_T>>().ToList();
Although you certainly could call OfType<> method using reflection:
var list = new List<IMyClass> { ... };
var item1_T = typeof(int);
var subset = (typeof(Enumerable)
.GetMethod(nameof(Enumerable.OfType))
.MakeGenericMethod(typeof(MyClass<>).MakeGenericType(item1_T))
.Invoke(null, new object[]{ list }) as IEnumerable<IMyClass>)
.ToList();
But that would be quite inefficient.
A much faster way would be:
var typeToFind = typeof(MyClass<>).MakeGenericType(item1_T);
var subset = list.Where(item => item != null && item.GetType() == typeToFind).ToList();
If this code is not performance critical, probably it wouldn't matter if you use this solution, otherwise I'd suggest to refactor the code to avoid using reflection.
In IMyClass add this line:
bool AreInterchangable(IMyClass otherInstance);
Implement in MyClass<`T>:
public bool AreInterchangable(IMyClass otherInstance)
{
return otherInstance is IMyClass<T>;
}
Instead of var subset = list.OfType<MyClass<item1_T>>().ToList(); use this:
var subset = list.Where(item => item.AreInterchangable(item1));
Note that I also introduced a generic interface IMyClass<T> which sits between IMyClass and MyClass<T>

This foreach has a cast in the middle, can it be translated to linq?

Resharper is always asking me to change foreach loops into linq, and I try to. This one stumped me.
foreach(var fileEnvironment in fileEnvironmentSection.FileEnvironmentList) {
var element = fileEnvironment as FileEnvironmentElement;
if(element == null) {
//Not the expected type, so nothing to do
} else {
//Have the expected type, so add it to the dictionary
result.Add(element.Key, element.Value);
}
}
The fileEnvironment is returned as an object from the configuration section. That's the reason for the cast.
You can use the OfType operator:
Filters the elements of an IEnumerable based on a specified type.
foreach(var element in fileEnvironmentSection.FileEnvironmentList
.OfType<FileEnvironmentElement>())
{
result.Add(element.Key, element.Value);
}
LINQ isn't going to help with the body of the loop since it's mutating an existing dictionary - LINQ is normally used for creating new data rather than modifying existing data.
If you don't mind returning a new dictionary here, you could do:
var newResult = fileEnvironmentSection.FileEnvironmentList
.OfType<FileEnvironmentElement>()
.ToDictionary(element => element.Key,
element => element.Value);

C# Extension method instead of iteration

I was wondering, if there is an extension method that allow me to iterate a List and let me do the same thing with every item in the list. For example:
.RemoveAll(x => x.property == somevalue)
This removes every element wichs fulfill the condition. But, what if I have this:
foreach(object item in lstObjects)
{
object MyObject = new object();
MyObject.propertyone = item.property
MyObject.propertytwo = somevalue;
anotherlist.Add(MyObject);
}
Of course, the real code is a little more complex than this. My objective is to, instead of a foreach use an extension method, I have found List<T>.ForEach() but I can't get it to work, and this method does not exist in a var list. I found too .Select<>, .Where<> but this returns values, and in my method there is no need to return any value.
var convertedItems = lstObjects.Select(item =>
{
object MyObject = new object();
MyObject.propertyone = item.property
MyObject.propertytwo = somevalue;
return MyObject;
});
anotherList.AddRange(convertedItems);
or
anotherList = convertedItems.ToList();
and if you want to make it shorter:
var convertedItems = lstObjects.Select(item =>
new object {propertyone = item.property, propertytwo = somevalue});
I'm not sure I see why you want an extension method here. List<T>.ForEach() will do mostly what you like but your existing foreach loop is both idiomatic and readable. Is there a reason that you can't just write a normal function to do this?
public void DoMyThing(IList<object> objects) {
foreach (var obj in objects) {
someOtherList.Add(new MyObj() {
item1 = obj
});
}
}
In general if you find that you need to mutate items and not return values you don't want to use LINQ or query operators. Just use a foreach.
Edit: The answers suggesting Select() would work for this simple code, however you state
the real code is a little more complex than this
Which suggests to me that you may have to mutate some other state during iteration. The Select method will defer this mutation until the sequence is materialized; this will probably give you strange results unless you're familiar with how LINQ queries defer execution and capture outer variables.
It's trivial to write your own ForEach extension. I include the following in all of my code:
public static void ForEach<T>(this IEnumerable<T> collection, Action<T> action )
{
foreach (T item in collection)
{
action(item);
}
}
You can accomplish this via a Select statement:
var newList = lstObjects.Select(o =>
new { propertyone = o.property,
propertytwo = somevalue }).ToList();
Here is how you use ForEach with a lambda expression:
lstObjects.ForEach(item =>
{
MyObject obj = new MyObject();
obj.propertyone = item.property;
obj.propertytwo = somevalue;
anotherlist.Add(obj);
});
However as you can see it looks remarkably similar to what you already have!
Alternatively it looks to me like Select might be a better match for what you want to do:
anotherList.AddRange(lstObjects.Select(item => new MyObject()
{
propertyone = item.property,
obj.propertytwo = somevalue,
}));
List<MyObjectType> list = new List<MyObjectType>();
list.ForEach((MyObjectType item) => {
object MyObject = new object()
{
MyObject.propertyone = item.property,
MyObject.propertytwo = somevalue
};
anotherlist.Add(MyObject);
});
If you want to perform an action as part of an iteration, you might want to consider the .Do method which is part of the Interactive Extensions. See http://www.thinqlinq.com/Post.aspx/Title/Ix-Interactive-Extensions-return.
You can easily create an extension method to do this:
public IEnumerable<T> RemoveAll(this List<T> list, Func<bool, T> condition)
{
var itemsToRemove = list.Where(s => condition(s));
list.RemoveAll(itemsToRemove);
}
and you could then call it like this:
myList.RemoveAll(x => x.Property == someValue);
Edit: Here is another method for doing the same.
As far as 'built-in' goes there is no .ForEach(); however I think .Aggregate() would be the most appropriate option here (if you absolutely and utterly want a built-in function).
lstObjects.Aggregate(anotherList, (targetList, value) =>
{
object MyObject = new object();
MyObject.propertyone = item.property
MyObject.propertytwo = somevalue;
targetList.Add(MyObject);
return targetList;
});
You can obviously just write your own extension methods:
public static IEnumerable<T> Intercept<T>(this IEnumerable<T> values, Action<T> each)
{
foreach (var item in values)
{
each(item);
yield return item;
}
}
public static IEnumerable<T> Intercept<T>(this IEnumerable<T> values, Action<T, int> each)
{
var index = 0;
foreach (var item in values)
{
each(item, index++);
yield return item;
}
}
// ...
a.Intercept(x => { Console.WriteLine(x); }).Count();
NB: The reason I don't create a ForEach like everyone else, is because Microsoft didn't include it because, by design Linq methods always return a value, or list of values.
Specifically to your question, .Select<T> will do the trick.
anotherList.AddRange(lstObjects.Select(x => new MyObject()
{
propertyone = x.property,
propertytwo = somevalue
}));

Efficient way of updating a collection from another collection

Is the following way of updating an ObservableCollection from another one (both based on the same class) good enough or it better to be done in another way (or just to be improved)?
foreach (MyEntity c in collection2)
{
collection1.Where(p => p.EntID == c.EntID).FirstOrDefault().Field1 = c.Field1;
collection1.Where(p => p.EntID == c.EntID).FirstOrDefault().Field2 = c.Field2;
...
collection1.Where(p => p.EntID == c.EntID).FirstOrDefault().FieldN = c.FieldN;
}
EntID is the primary key.
(Under good enough I mean fast and efficient).
var myItem = collection1.Where(p => p.EntID == c.EntID).FirstOrDefault();
if (myItem == null)
continue;
myItem.Field1 = c.Field1;
myItem.Field2 = c.Field2;
...
myItem.FieldN = c.FieldN;
If myItem and c are different types, have a look at AutoMapper.
As a complementary answer, you can use reflection to copy the N fields from one object to another. I've already talked about this here: How to refactor this? .
You can have your class (SomeClass) implement this code (both objects are the same class):
public void CopyPropertiesFrom(SomeClass SourceInstance)
{
foreach (PropertyInfo prop in typeof(SomeClass).GetProperties())
prop.SetValue(this, prop.GetValue(SourceInstance, null), null);
}
That way, if your class has new properties, you don't have to bother updating the code, it's already there!
For objects with different class, that's also doable via reflection by the property name, but there are some assumptions you have to make (what if property does not exist, what property is different type, what is property value is null, etc.)

C# List - Group By - Without Linq

I have an object:
IObject
{
string Account,
decimal Amount
}
How do I group by Account and Sum the Amount, returning a List without Linq.
2.0 Framework ... that is why no Linq.
Here is what I have:
ListofObjects = List<IObject>;
foreach (var object in objects)
{
var objectToAdd = new Object(object);
var oa = ListofObjects.Find(x => x.Account == objectToAdd.Account);
if (oa == null)
{
ListofObjects.Add(objectToAdd);
}
else
{
ListofObjects.Remove(oa);
oa.Amount = objectToAdd.Amount;
ListofObjects.Add(oa);
}
}
Easiest answer: use LINQBridge and get all your LINQ to Objects goodness against .NET 2.0... works best if you can use C# 3 (i.e. VS2008 but targeting .NET 2.0).
If you really can't do that, you'll basically need to keep a dictionary from a key to a list of values. Iterate through the sequence, and check whether it already contains a list - if not, add one. Then add to whatever list you've found (whether new or old).
If you need to return the groups in key order, you'll need to also keep a list of keys in the order in which you found them. Frankly it's a pain... just get LINQBridge instead :)
(Seriously, each individual bit of LINQ is actually fairly easy to write - but it's also quite easy to make off-by-one errors, or end up forgetting to optimize something like Count() in the case where it's actually an ICollection<T>... There's no need to reinvent the wheel here.)
EDIT: I was about to write some code, but then I noticed that you want a list returned... a list of what? A List<IList<IObject>>? Or are you actually trying to group and sum in one go? If so, don't you want a list of pairs of key and amount? Or are you going to reuse the same class that you've already got for a single account, but as the aggregate? If it's the latter, here's some sample code:
public static IList<IObject> SumAccounts(IEnumerable<IObject> data)
{
List<IObject> ret = new List<IObject>();
Dictionary<string, IObject> map = new Dictionary<string, IObject>();
foreach (var item in data)
{
IObject existing;
if (!map.TryGetValue(item.Account, out existing))
{
existing = new IObject(item.Account, 0m);
map[item.Account] = existing;
ret.Add(existing);
}
existing.Amount += item.Amount;
}
return ret;
}
Admittedly the extra efficiency here due to using a Dictionary for lookups will be pointless unless you've got really quite a lot of accounts...
EDIT: If you've got a small number of accounts as per your comment, you could use:
public static IList<IObject> SumAccounts(IEnumerable<IObject> data)
{
List<IObject> ret = new List<IObject>();
foreach (var item in data)
{
IObject existing = ret.Find(x => x.Account == item.Account);
if (existing == null)
{
existing = new IObject(item.Account, 0m);
ret.Add(existing);
}
existing.Amount += item.Amount;
}
return ret;
}
Use a dictionary to hold the results. Locating an item in a dictionary is close to an O(1) operation, so it's a lot faster than searching for items in a list.
Dictionary<string, decimal> sum = new Dictionary<string, decimal>();
foreach (IObject obj in objects) {
if (sum.ContainsKey(obj.Account)) {
sum[obj.Account].Amount += obj.Amount;
} else {
sum.Add(obj.Account, obj.Amount);
}
}

Categories