WhereClipBuilder是在1.7.2版本中新增的一个类,用来创建WhereClip。
在之前版本多条件创建WhereClip如下:
WhereClip where = WhereClip.All; where = where.And(Products._.ProductName.Contain("apple")); where = where.And(Products._.UnitPrice > 1); where = where.Or(Products._.CategoryID == 2);
每增加一个条件都是生成一个新的WhereClip。
使用WhereClipBuilder如下:
WhereClipBuilder wherebuilder = new WhereClipBuilder(); wherebuilder.And(Products._.ProductName.Contain("apple")); wherebuilder.And(Products._.UnitPrice > 1); wherebuilder.Or(Products._.CategoryID == 2);
WhereClipBuilder是条件累加并不增加条件而创建新实例, 从而得到重用,节省资源。
具体使用:
Northwind.From<Products>()
.Where(wherebuilder.ToWhereClip())
.ToList();
生成的sql:
Text:
SELECT * FROM [Products]
WHERE (((([Products].[ProductName] LIKE @jtioerxooyxzyvsj) AND ([Products].[UnitPrice] > @txdupnwvhfznviqj))
OR ([Products].[CategoryID] = @iqgkjykstppcrqsq)))
Parameters:
@jtioerxooyxzyvsj[String] = %apple%
@txdupnwvhfznviqj[Int32] = 1
@iqgkjykstppcrqsq[Int32] = 2