I am trying to find the best way to build a dynamic linq query and populate a list. The user will have about five different fi开发者_运维技巧elds in which they can choose to filter on. Currently i have the following code:
List<TBLPROMOTION> promotionInfo = null;
bool active = true;
int storeId = 1
using (WSE webStoreContext = new WSE())
{
promotionInfo =
webStoreContext.TBLPROMOTION.Include("TBLSTORE").Include("LKPROMOTIONTYPE")
.Where("STORE_ID==" + storeId + " and IS_ACTIVE == " + active).ToList();
}
However, this example errors out because it cannot read the fields referenced in the where clause. Not sure why just saw this example somewhere else and tried to emulate it. My question is does anyone have an example that will work for my situation?
Thanks in advance, Billy
You probably were thinking of Dynamic LINQ. It will allow you to construct queries dynamically.
There's a temptation to use dynamic queries they use a more familiar SQL syntax. I would recommend against using them. When you use them you can use the type safety and protection against SQL injection attacks that Linq provides.
You can almost always construct a query using normal Linq syntax without the dynamic component. And it's worth the struggle to learn Linq.
Went with the following example on how to accomplish this task.
http://blog.bvsoftware.com/post/2008/02/27/How-to-create-a-Dynamic-LINQ-Query-Programmatically.aspx
精彩评论