How can I make this code better? It would be better if I could modify method GetItemsInfo() to send several types at one call.
using System.Collections.Generic;
using System.Linq;
public class SomeClass
{
List<Item> itemBase;
...
public IEnumerable<ItemInfo> SomeMethod()
{
return itemBase.GetItemsInfo<GeneralItem>()
.Concat(itemBase.GetItemsInfo<FirstTypeItem>())
.Concat(itemBase.GetItemsInfo<SecondTypeItem>())
.Concat(itemBase.GetItemsInfo<ThirdTypeItem>());
}
}
where
public class ItemInfo
{...}
public class GeneralItem :ItemInfo
{...}
public class FirstTypeItem :ItemInfo
{...}
...
List<ItemInfo> items;
...
public IEnumerable<ItemInfo> GetItemsInfo<T>() where T: ItemInfo
{
return items.Where(item => item.GetType().Equals(typeof(T)));
}
Thanks!
Update. Question updated by concrette realization of GetItemsInfo.
One more update. For now I wrote non-generic method
public IEnumerable<ItemInfo> GetItemsInfo(params System.Type[] types)
{
return items.Where(item => types.Any(type => type.Equals(item.GetType())));
}
But It isn't looks pretty. Because in this array user can add any Type he wants even not child of ItemInfo. Maybe it's not a problem cause items is the collection of ItemInfo objects.