简体   繁体   中英

Linq Select dynamically from generic <T> List

I want to get a list of each object from my List<T> (except strings, ints etc). And then Invoke (generic, recursive method with reflection). The problem is I am iterating on the property names, and have no idea how to select.

Error CS0021 Cannot apply indexing with [] to an expression of type 'T'

Code:

public static void My method<T>(IEnumerable<T> query)
{
    var t = typeof(T);
    var Headings = t.GetProperties();

    for (int i = iteratorStart; i < Headings.Count(); i++)
    {
        if (IsValue(Headings[i].PropertyType.FullName))
        {
        }
        else
        {
           Type type = Type.GetType(Headings[i].PropertyType.FullName);
           var mi = typeof(ExcelExtension);
           var met  = mi.GetMethod("ListToExcel");
           var genMet = met.MakeGenericMethod(type);

           var nested = query.Select(p => p[Headings[i].Name]);

           object[] parametersArray = new object[] { pck, nested, i };
           genMet.Invoke(null, parametersArray);
        }
    }
}

As far as I can see, this is what you want:

public static void Mymethod<T>(IEnumerable<T> query)
{
    var t = typeof(T);
    int pck = 1234;

    var mi = typeof(ExcelExtension);
    var met = mi.GetMethod("ListToExcel");

    var Headings = t.GetProperties();
    for(int i=0; i < Headings.Length; ++i)
    {
        var prop = Headings[i];
        if (prop.PropertyType.IsClass)
        {
            var genMet = met.MakeGenericMethod(prop.PropertyType);

            var nested = query.Select(p => prop.GetValue(p));

            object[] parametersArray = new object[] { pck, nested, i };
            genMet.Invoke(null, parametersArray);
        }
    }

}


class ExcelExtension
{
    public void ListToExcel<T>(int pck, IEnumerable<object> nested, int i)
    {

    }
}

Assuming you are using c# 6.0 or higher. You can use generic type parameters like;

public static void MyMethod<T>(IEnumerable<T> query) where T : IList
{
    //Your code here
}

This way, you ensure that T is List of something and reaching indexing won't be a problem.

UPDATE

I misunderstood the question earlier. Here is the updated solution.

    public static void MyMethod<T>(IEnumerable<T> query)
    {
        var t = typeof(T);
        var Headings = t.GetProperties();
        for (int i = iteratorStart; i < Headings.Count(); i++)
        {
            if (false == IsValue(Headings[i].PropertyType.FullName))
            {
               Type type = Type.GetType(Headings[i].PropertyType.FullName);
               var mi = typeof(ExcelExtension);
               var met  = mi.GetMethod("ListToExcel");
               var genMet = met.MakeGenericMethod(type);
               //Assuming you want to get property value here. IF not You can use like Headings[i].GetName
               var nested = query.Select(p =>Convert.ChangeType( Headings[i].GetValue(p),Headings[i].GetType()));
               object[] parametersArray = new object[] { pck, nested, i };
               genMet.Invoke(null, parametersArray);
            }
        }

}

Error Explanation: The problem is in the Select(p => p[something here]) part. Since p is not the property list or array but a type of object, it doesn't contain any indexer. You should use reflection like above example.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM