jeudi 10 janvier 2019

Dynamic linq for object collection - Target object is not an ExpandoObject

I try to dynamically read the property values based on the text naming of these properties. This simply works for nested classes. But if there is a nested collection class, I would like to be able to select an item using a dynamic linq. Unfortunately, while reading in the collection, it returns 'Target object is not an ExpandoObject' (.FirstOrDefault).

Any idea to edit or possibly another solution?

My Code (.net core 2.1):

string value = string.Empty;

var dataClassProperty = dataClass.GetType().GetProperty(dataClassPropertyName).GetValue(dataClass, null);
if (dataClassProperty is IEnumerable<object>)
{
    object nestedPropertyItem = (dataClassProperty as IEnumerable<object>)
                                .AsQueryable()
                                .FirstOrDefault(collectionDynamicLinq);

    if (nestedPropertyItem == null)
        value = string.Empty;
    else
        value = nestedPropertyItem.GetType().GetProperty(nestedPropertyName).GetValue(nestedPropertyItem, null)?.ToString() ?? "";
}

Thanks!





Aucun commentaire:

Enregistrer un commentaire