Last post Oct 22, 2014 05:38 AM by Kevin Shen - MSFT
Oct 21, 2014 04:09 PM|briankitt|LINK
I am sure that there are a 1000 better ways to do this, but I have been searching, and I am just having trouble finding a better way. I have a search panel, where the user can enter any number of parms. There are about 20 right now. We allow them to
use a '%' to specify wild cards. Now, I realize I can just easily write standard query syntax, but my directive is to use LINQ whenever possible. So I want to do this in LINQ if I can.
For each of the 20, I have code like this: (myModel is the model from an MVC form, and myMBrs is a linq object to the Member database. It would seem to me I should be able to write a simple prototype or interface that I could call 20 times with the Column
Name and the Screen field, rather than doing this code 20 times. But I am not able to get the dynamic LINQ to resolve properly.
if (myModel.NAMEF.StartsWith("%") && myModel.NAMEF.EndsWith("%"))
myMBrs = myMBrs.Where(s => s.NAMEF.Contains(myModel.NAMEF.Substring(1, myModel.NAMEF.Length - 2)));
else if (myModel.NAMEF.StartsWith("%"))
myMBrs = myMBrs.Where(s => s.NAMEF.EndsWith(myModel.NAMEF.Substring(1)));
else if (myModel.NAMEF.EndsWith("%"))
myMBrs = myMBrs.Where(s => s.NAMEF.StartsWith(myModel.NAMEF.Substring(myModel.NAMEF.Length - 1)));
myMBrs = myMBrs.Where(s => s.NAMEF == myModel.NAMEF);
Oct 22, 2014 05:38 AM|Kevin Shen - MSFT|LINK
It seems that you can use Expression Trees to Build Dynamic Queries for your project.
I suggest that you can refer to the link below: