I have a rather extensive view that needs to pull info for some kind of "search engine". I access this view through Entity framework (with the use of a repository). I use a class name searchprops that contains 2 strings, a propertyName, and a property value.
Allow me to demonstrate the problem I am struggling with. In my head, the following solution should have worked, Using the "propertyName" value and utilise reflection to fill the correct property with the correct value, Like sending "vin", "123", should have built where X.Vin == 123... Ofcourse, This triggers an error stating sql to entities can not be used with reflection, and this just baffles me.... Would somebody please point me in the right direction? (explicitly setting a property does work however. so not all of the code is bad atleast, Its just reflection.)
public List<vw_AdHocReporting> GetDataFromView(IDictionary<string, string> actualValuesDictionary)
{
var searchprops = actualValuesDictionary.Select(keypair => new SearchProp {PropertyName = keypair.Key, PropertyValue = keypair.Value}).ToList();
var predicate = PredicateBuilder.New<vw_AdHocReporting>();
foreach (var prop in searchprops)
{
predicate = predicate.Or(p => p.GetType().GetProperty(prop.PropertyName).Name.Contains(prop.PropertyValue));
}
var query = context.vw_AdHocReporting.AsExpandable().Where(predicate).ToList();
return query;
}
Aucun commentaire:
Enregistrer un commentaire