Reputation: 184
I am writing a dynamic linq query to realize paging, and I am now facing a problem, I need the System.Linq.Expressions.Expression.Like
function, but it does not exist in System.Linq.Expressions.Expression
, here is my code.
Expression mWhereFunc; // Filter clause
ParameterExpression mLinqParam; // Linq param
// Get current request page
string mCurPage = this.Request.QueryString["page"];
if (String.IsNullOrEmpty(mCurPage))
{
mCurPage = "1";
}
mLinqParam = Expression.Parameter(typeof(ORD_Order), "p");
mWhereFunc = Expression.Equal(Expression.Property(mLinqParam,
typeof(ORD_Order).GetProperty("ItemIsValid")),
Expression.Constant(true));
string mOrderSN = this.Request.QueryString["txtOrderSN"];
if (!String.IsNullOrEmpty(mOrderSN))
{
mWhereFunc = Expression.And(mWhereFunc,
**Expression.Equal**(Expression.Property(mLinqParam,
typeof(ORD_Order).GetProperty("OrderSN")),
Expression.Constant(mOrderSN)));
}
var mLambdaWhere = (Expression<Func<ORD_Order,
bool>>)Expression.Lambda<Func<ORD_Order, bool>>(mWhereFunc,
new ParameterExpression[] { mLinqParam });
Func<ORD_Order, Int32> mLambdaOrder = p => p.OrderID;
IORD_OrderRepository rptOrder = new ORD_OrderRepository();
ICTM_CustomerRepository rptCtm = new CTM_CustomerRepository();
var list = from o in rptOrder.GetAll()
.Where(mLambdaWhere)
.OrderBy(mLambdaOrder)
.Skip((int.Parse(mCurPage) - 1) * mPageSize).Take(mPageSize)
join c in rptCtm.GetAll()
on o.CustomerID equals c.CustomerID
select new
{
o.OrderID,
o.OrderSN,
o.CustomerID,
c.ContactName,
o.Status,
o.CreateDate,
o.Description
};
Expression.Equal is the place where I want to change it to Expression.Like
Any help will be appreciated.
Upvotes: 1
Views: 1838
Reputation: 184
I modified my code like this
mWhereFunc = Expression.And(mWhereFunc, Expression.Call(
Expression.Property(mLinqParam, typeof(ORD_Order).GetProperty("OrderSN")),
typeof(String).GetMethod("Contains"),
new Expression[] { Expression.Constant(mOrderSN) }));
and it works, many thanks to all of you.
Upvotes: 2
Reputation: 12241
Like equivalents for linq are String.Contains
, String.StartsWith
etc. http://www.simonrhart.com/2008/06/using-like-in-linq-to-sql-under-c.html
Upvotes: 1
Reputation: 3713
if you are using NHibernate you can create a Like method and use a Expression to call that
You can check this post on how to do the "Like" extension here: NHibernate Linq Provider Extension
And then you build the "Like" Expression like this:
Expression.Call(typeof(MyLinqExtensions).GetMethod("IsLike"), Expression.Property(mLinqParam,
typeof(ORD_Order).GetProperty("OrderSN")),
Expression.Constant(mOrderSN)));
EDIT: Just as an additional comment, you should use Expression.AndAlso
instead of Expression.And
since the first one is the && operator and the last one is the & operator
EDIT 2: For Entity Framework check this post (Linq To Entities scenario): SQL User-Defined Functions in Entity Framework 4, I have no experience with it but it seems the same as a NH provider, after doing that then building the Linq Expression should be the same i posted before
Upvotes: 0