c# - 'System.String' 类型的表达式不能用于'System.Reflection.PropertyInfo' 类型的参数
问题描述
我有两个类 - ContactCompany 和 ContactPeople 列表。
结果必须是 - 符合特定条件的所有联系人或特定联系人的列表。
条件是一个字符串,它将搜索两个类中的所有字符串字段。如果找到 ContactCompany ,将显示所有联系人列表。
到目前为止,我想出了这个:
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;
}
我的想法是检查属性及其值,ToString() 并将其与用户输入的标准进行比较。
只是另一个注意事项 - 我写了 prop.Declarintype.Name 以排除 AuditEntity 属性。(创建者、创建者等)
当我点击 allContactPersonFoundInCompany 时,无法翻译 ToString()。
这是我收到的完整错误:
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')
解决方案
我理解反射的含义,它很慢,因此这是我的解决方案。
public List<ContactPersonDto> FilterContragentAndClients(string filter)
{
var contactPeopleInCompany = this.contactCompanyRepository.GetAll
(c => c.AccountablePerson.Contains(filter) |
c.Address.Contains(filter) ||
c.City.Contains(filter) ||
c.Name.Contains(filter) ||
c.Prefix.Contains(filter) ||
c.RegistrationNumber.Contains(filter)).SelectMany(c => c.ContactPeople);
var contactPerson = this.contactPersonRepository.GetAll
(cp => cp.Address.Contains(filter) ||
cp.Email.Contains(filter) ||
cp.Name.Contains(filter) ||
cp.PhoneNumber.Contains(filter) ||
cp.Prefix.Contains(filter));
var together = contactPeopleInCompany.Concat(contactPerson).Distinct().Take(100).ToList();
List<ContactPersonDto> result = new List<ContactPersonDto>();
foreach (var item in together)
{
result.Add(mapper.Map<ContactPersonDto>(item));
}
return result;
}
推荐阅读
- c# - How to reduce the time to switch node connection (in cluster) when a node is down
- ruby - Ruby PageObject Design for Similar Page Sections
- java-native-interface - Read/write to memory that was previously allocated via JNI
- spring-boot - Use different file path for testing in Spring Boot
- javascript - Flow restrict to extend a class by assigment methods from callback
- javascript - Jqplot - 无法读取未定义的属性“startAngle”
- wpf - NSSM 无法启动 windows 服务
- react-native - 如何调查发布包崩溃?
- angular - 查看单选按钮是否被选中
- java - 在 Dropwizard 中的不同状态下使用不同的模拟 Pact 提供程序