Here is a simple extension method that you can create to extract only the types you need:
public static class Extensions { public static IEnumerable<U> ExtractOfType<U, T>(this IEnumerable<T> list) where T : class where U : class { foreach (var item in list) { if (typeof(U).IsAssignableFrom(item.GetType())) { yield return item as U; } } } }
Test:
public interface IBaseInterface { string Foo { get; } } public interface IChildInterface : IBaseInterface { string Foo2 { get; } } public interface IOtherChildIntreface : IBaseInterface { string OtherFoo { get; } } public class BaseImplementation : IBaseInterface { public string Foo { get { return "Foo"; } } } public class ChildImplementation : IChildInterface { public string Foo2 { get { return "Foo2"; } } public string Foo { get { return "Foo"; } } } public class OtherChildImplementation : IOtherChildIntreface { public string OtherFoo { get { return "OtherFoo"; } } public string Foo { get { return "Foo"; } } }
....
List<IBaseInterface> b = new List<IBaseInterface>(); b.Add(new BaseImplementation()); b.Add(new ChildImplementation()); b.Add(new OtherChildImplementation()); b.Add(new OtherChildImplementation()); foreach (var s in b.ExtractOfType<IOtherChildIntreface, IBaseInterface>()) { Console.WriteLine(s.GetType().Name); }
This will get all the items in the list that have the derived type that you are looking for. So, in your controller, go all the way to the view. Then, partial views that accept an IEnumerable of the type required by the partial, and in your main view, call this extension method and pass the result to these separate partial views.
Bfree source share