C# 获取List[]数组中获取某个类型的集合数据

场景假设:请结合自身情况修改!

假设有两个集合,集合类型分别是【PhysicalResult】和【PhysicalSummary】

IEnumerable results = Current.Where(resultfilter).OrderBy(a =>a.CompositeItem.Id).ToList();
IEnumerable summaries = Current.Where(a => a.Summary != null).OrderBy(a => a.Id).ToList();

添加到List[]中

IList[] list = new IList[]{results.ToList(), summaries.ToList()};

若想取出类型为【PhysicalResult】的数据集合

方案一:

foreach (var item in list)
{
    Type type = item.GetType();
    if (type.IsGenericType)//是否是泛型
    {
       Type[] genericTypes = type.GetGenericArguments();//获取类名
       string ClassName = genericTypes[0].Name;
       if (ClassName == "IPhysicalResult")
       {
           results = item as List;
       }
       else if (ClassName == "IPhysicalSummary")
       {
           summaryLists = item as List;
       }
     }
 }

方案二:

IList results = list.CastAs>().FirstOrDefault();
IList summaryLists = list.CastAs.().FirstOrDefault();

 

你可能感兴趣的:(C#)