I don't have Visual Studio manually to test it, but this should work too:
var contactProperties = typeof(Contact).GetProperties() .Where(x => x.PropertyType == typeof(string)) .ToArray(); from contact in context.tblContacts from property in contactProperties let value = property.GetValue(contact, null) as string where value == searchText select contact
I would get the properties only once, because it is the same every time.
To your question whether to use reflection or not .. it depends. If your Contact type has only a few (2-5) string properties, I would use this method manually instead of using reflection. It may be a bit tedious, but it pays for readability, speed (but there shouldn't be any noticeable difference), and you can control which properties are compared (maybe your Contact contains a string property that you donβt need to look for).
If you plan to extend / change the Contact type or you need to check a large piece of properties, I would go with a reflection.
source share