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.
I think you should be able to do this using just a Func<TIn,TOut> parameter (expressions not needed in this case). Make the function generic to be type safe whatever the type of the column might be. Here's what I'm thinking ...
Then you'd invoke it like this:
Hands down, LINQ expressions are the best way to dynamically build LINQ queries in a strongly typed manner. You are absolutely right to discard the Dynamic LINQ Library! LINQ Expressions are challenging to grasp at first, but I promise you that the end pay off is well worth the effort.
Here is an example that uses LINQ expressions to accomplish what you want. You'll notice it doesn't include any string column names, switch statements, helper classes, or enums. You will need to import the
System.Linq.Expressions
namespace for this to work:EDIT: The example now includes filtering by a column on one joined table, while selecting an element from another. I also removed the
investments
parameter from the method, as you don't actually need to pass that in. You are just accessing the EF tables directly in the method (which I substitute for_performance
and_investments
).You would call
PerformanceSearch
this way, using this simple console app as an example:This example is generic enough to allow you to pass a
double
property fromPerformance
assearchColumn
, specifying min and max values asdouble
.another option is something we used for a dynamic reporting system, on the fly code-generation and compilation:
http://msdn.microsoft.com/en-us/library/microsoft.csharp.csharpcodeprovider.aspx
You could build a dictionary containing your strongly-typed where clauses like so:
The complete method would look like this:
Building the dictionary for each call is blazingly fast compared to the actual database call so don't worry too much about it. If you do decide to worry then make the dictionary a static private field.