I am seeking help on how to achieve this with LINQ in a type safe way.
I need to perform search on "Performance" table with many columns. Based on the criteria specified for search I need to pick columns and perform search on those columns with given values.
private static IQueryable<Investment> PerformanceSearch(IQueryable<Investment> investments, **??? searchColumn**, double minValue, double maxValue)
{
var entity = ExtendedEntities.Current;
investments = from inv in entity.Investments
join performance in entity.Performances on inv.InvestmentID equals perfromance.InvestmentID
where **performance.searchColumn** >= minValue && **performance.searchColumn** = maxValue
return investments;
}
Now I am seeking your help on:
How to pass column "searchColumn" to this method in a type safe way? I was thinking of creating a dictionary object to accommodate some way to maintain column names from entity framework. But not sure how to achieve this.
How to perform LINQ query using the columnName passed and applying where clause.
I cannot use If Else or Switch case as below is a list of possible search columns...
/*
* Search Columns can be:
* "Return1Month", "Return2Months", "Return3Months", ... almost 10 more and
* "Risk1Month", "Risk2Months", "Risk3Months", ... almost 10 more and
* "TrackingError1Month", "TrackingError2Months", "TrackingError3Months", ... almost 10 more and
* 2 more similar set of columns ...
*/
I spent time on Stackoverflow, Microsoft and other blogs and considered using Dynamic LINQ but it's not type safe. It seems that it is achievable using expressions but couldn't get that working.
Any advice is appreciated.
EDIT -
Another item to mention - all search columns are present in "performance" table.