jeudi 27 juin 2019

How to get property of class that is nested in IGrouping collection

I created a generic method to sort. I took type of list, then lookup of prop name that is equal to sortorder from view. Now I have a case with IGrouping collection - for example List<IGrouping<int, ViewModel>> and I dont know how to sort by props from ViewModel which is nested in IGrouping

public static List<IGrouping<int, T>> SortOrder<T>(List<IGrouping<int, T>> list, string sortOrder) where T : new()
{
    Type listType = AssemblyHelper.GetCollectionType(list);
    if (listType.Name.Contains("IGrouping")) { listType = listType.GenericTypeArguments[1]; }

    foreach (var prop in listType.GetProperties())
    {
        if (prop.Name.ToLower() == sortOrder)
        {
            if (AssemblyHelper.GetCollectionType(list).Name.Contains("IGrouping"))
            {
                return //How to OrderBy ViewModel prop that is equal to sort order
            }
            else
            {
                return list.OrderBy(x => prop.GetValue(x, null)).ToList();
            }
        }
    }
    return default(List<IGrouping<int, T>>);
}





Aucun commentaire:

Enregistrer un commentaire