Search on all fields of an entity

后端 未结 1 763
温柔的废话
温柔的废话 2021-02-05 11:03

I\'m trying to implement an \"omnibox\"-type search over a customer database where a single query should attempt to match any properties of a customer.

Here\'s some samp

1条回答
  •  长情又很酷
    2021-02-05 11:38

    first find all properties within Customer class with same type as query:

    var stringProperties = typeof(Customer).GetProperties().Where(prop =>
        prop.PropertyType == query.GetType());
    

    then find all customers from context that has at least one property with value equal to query:

    context.Customer.Where(customer => 
        stringProperties.Any(prop =>
            prop.GetValue(customer, null) == query));
    

    0 讨论(0)
提交回复
热议问题