lundi 22 juin 2020

'Expression of type 'System.String' cannot be used for parameter of type 'System.Reflection.PropertyInfo'

I have two classes - ContactCompany and inside List of ContactPeople.

The result must be - list of all contact people or a specific contact person that matches a certain criteria.

The criteria is a string and it will search all the string fields in both classes. If a ContactCompany is found , all the list of contact people will be displayed.

So Far I came up with this:

public List<ContactPersonDto> FilterContragentAndClients(string filter)
{
    var contactCompanyStringProperties = typeof(ContactCompany).GetProperties().Where(prop => prop.PropertyType == filter.GetType() && prop.DeclaringType.Name != "AuditEntity`1");
    var contactPersonStringProperties = typeof(ContactPerson).GetProperties().Where(prop => prop.PropertyType == filter.GetType());
    var together = contactCompanyStringProperties.Concat(contactPersonStringProperties);

    var allContactPersonFoundInCompany = this.contactCompanyRepository.GetAll(cc => contactCompanyStringProperties.Any
        (prop => ((prop.GetValue(cc, null) == null) ? "" : prop.GetValue(cc, null).ToString().ToLower()) == filter)).SelectMany(acc => acc.ContactPeople).ToList();

    var contactPersonOnItsOwn = contactPersonRepository.GetAll(cp => contactPersonStringProperties.Any
        (prop => ((prop.GetValue(cp, null) == null) ? "" : prop.GetValue(cp, null).ToString().ToLower()) == filter));

    var totalList = allContactPersonFoundInCompany.Concat(contactPersonOnItsOwn).Distinct().ToList().Take(100);

    List<ContactPersonDto> result = new List<ContactPersonDto>();
    foreach (var item in totalList)
    {
        result.Add(mapper.Map<ContactPersonDto>(item));
    }
    return result;
}

My idea was to check the property and its value, ToString() it and compare it with the criteria the user has inputted.

Just another note - I wrote the prop.Declarintype.Name in order to exclude AuditEntity properties.(Created By, Created At, etc.)

When I hit allContactPersonFoundInCompany the ToString() cannot be translated.

This is the full error I receive:

Expression of type 'System.String' cannot be used for parameter of type 'System.Reflection.PropertyInfo' of method 'Boolean Contains[PropertyInfo](System.Collections.Generic.IEnumerable`1[System.Reflection.PropertyInfo], System.Reflection.PropertyInfo)' (Parameter 'arg1')




Aucun commentaire:

Enregistrer un commentaire