首页 > 其他 > 详细

动态生成lambda表达式

时间:2015-03-15 00:30:21      阅读:420      评论:0      收藏:0      [点我收藏+]

//1.0 获取我的申请单
int uid = UserMgr.GetCurrentLoginUser().uID;

//2.0 获取参数
string kname = f["kname"];
string status = f["status"];

//第一个条件: var query= c => c.fCreatorID == uid
//定义出一个c的参数,它的类型为wfReqeustForm
var ps = Expression.Parameter(typeof(wfRequestForm), "c"); //定义 =>左边的参数c
var uidvalue = Expression.Constant(uid); //定义比较的uid的值
// 实现=> 右边的 c.fCreatorID == uid,最终query表达式就是: c.fCreatorID == uid
var query = Expression.Equal(Expression.Property(ps, "fCreatorID"), uidvalue);

//3.0
if (!kname.IsEmpty())
{
//应该要动态构造一个lambda表达式query=query && c.wfRFTitle.Contains(kname)
//1.0 利用Expression动态构造出一个 c.wfRFTitle.Contains(kname) 表达式
var knameValue = Expression.Constant(kname);
//定义调用Contains 方法的Expression对象,leftwfRFTitle相当于c.wfRFTitle.Contains(kname)
var leftwfRFTitle = Expression.Call(
Expression.Property(ps, "wfRFTitle")
, typeof(string).GetMethod("Contains")
, knameValue);

//将query与leftwfRFTitle表达用&&的方法连接起来 结果就变成了: c.fCreatorID == uid && c.wfRFTitle.Contains(kname)
query = Expression.And(leftwfRFTitle, query);
}

if (status != "-1")
{
//应该要动态构造一个lambda表达式 query=query && c.wfRFStatus== status
var statusVal = Expression.Constant(int.Parse(status)); //定义比较的status的值
// 实现=> 右边的 c.fCreatorID == uid,最终query表达式就是: c.wfRFStatus == status
//== :Equal !=:NotEqual >:GreaterThan >=:GreaterThanOrEqual <=:LessThanOrEqual <:LessThan
var leftExpression = Expression.Equal(Expression.Property(ps, "wfRFStatus"), statusVal);
//Equal GreaterThan 大于 LessThan 小于 GreaterThanOrEqual大于等于
//将query与leftExpression 使用&& 连接
query = Expression.And(leftExpression, query);
}

// c=>c.uid==1 && (c.status==0 || c.status==1)

//根据动态构造的lambda表达式变成一个表达式树 c=>c.fCreatorID == uid && c.wfRFTitle.Contains(kname)
Expression<Func<wfRequestForm, bool>> queryLambda = Expression.Lambda<Func<wfRequestForm, bool>>(query, ps);

//2.0 查询申请单
var list = requestformSer.QueryJoin(queryLambda, new string[] { "sysKeyValue", "wfWork" })
.Select(c => new
{
c.wfRFID,
c.wfRFTitle,
c.wfWork.wfTitle,
c.wfRFPriority,
c.wfRFStatus,
c.sysKeyValue1.KName
,
c.wfRFRemark
})
.ToList()
.OrderBy(c => c.wfRFStatus) //40->41->42->43
.ThenByDescending(c => c.wfRFPriority); //39,38,37

return Json(new { Rows = list });

--------------------------------------
<label class="search-label">状态</label>
<div class="span2 search-input">
<select id="txtstatus">
<option value="-1">请选择</option>
<option value="40">审核中</option>
<option value="42">拒绝</option>
<option value="43">通过</option>
</select>
</div>



---------------------------------------------------

//1.0 获取分页参数和条件参数
//1.0.1 获取分页的页码和页容量
int page = f["page"].AsInt();
int pagesize = f["pagesize"].AsInt();
int tcount = 0;

//1.0.2 条件参数
string kname = f["kname"];
int wfid = f["wfid"].AsInt();

//c =>true && c.wfRFTitle.Contains(kname) && c.wfID == wfid
//c =>true && c.wfRFTitle.Contains(kname)
//c =>true &&c.wfID == wfid
//c=>true

//1.0.3 构造一个动态的lambda表达式树
//string sql = "select * from wfRequestForm where 1=1";
//if (!kname.IsEmpty())
//{
// sql += " and wfRFTitle like ‘%" + kname + "%‘ ";
//}
//if (wfid > 0)
//{
// sql += " and wfid = " + wfid;
//}

//定义c的变量,类型是 wfRequestForm
var c = Expression.Parameter(typeof(wfRequestForm), "c");
//构造 c=>true
var trueConst = Expression.Constant(true);
Expression where = trueConst;
//如果kname不为空,则应该动态构造出c.wfRFTitle.Contains(kname)
if (!kname.IsEmpty())
{
var knameConst = Expression.Constant(kname);
var query1 = Expression.Call(
Expression.Property(c, typeof(wfRequestForm).GetProperty("wfRFTitle"))
, typeof(string).GetMethod("Contains", new Type[] { typeof(string) })
, knameConst
); //c.wfRFTitle.Contains(kname)

//应该也true做and操作
where = Expression.And(trueConst, query1); // true && c.wfRFTitle.Contains(kname)
}

//判断wfid是否选择了某项,如果有则动态生成c.wfID == wfid
if (wfid > 0)
{
var wfidConst = Expression.Constant(wfid); //wfid
var wfidVer = Expression.PropertyOrField(c, "wfID"); //c.wfID
//将c.wfID与wfid做一个相等操作
var query2 = Expression.Equal(wfidVer, wfidConst); // c.wfID==wfid

where = Expression.And(where, query2);
}

//动态构造成最终的lambda表达式
Expression<Func<wfRequestForm, bool>> lambdaWhere;
lambdaWhere = Expression.Lambda<Func<wfRequestForm, bool>>(where, c);

//2.0 分页带条件查找数据
var list = requestformSer.QueryByPage(page, pagesize, out tcount, lambdaWhere, d => d.wfRFID)
.Select(cc => new
{
cc.wfRFID,
cc.wfWork.wfTitle,
cc.wfRFTitle,
cc.wfRFPriority,
cc.sysKeyValue1.KName,
cc.wfRFRemark
});

//3.0 返回以ligergrid要求的json格式
return Json(new { Rows = list, Total = tcount });
----------------------------------------------
<label class="search-label">名称</label>
<div class="span2 search-input">
<input id="kname" name="kname" type="text" value="">
</div>
<label class="search-label">工作流</label>
<div class="span2 search-input">
@Html.DropDownList("wfid", ViewBag.wfworks as SelectList)
</div>
----------------------------------------------

动态生成lambda表达式

原文:http://www.cnblogs.com/dianshen520/p/4338586.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!