wcf ria 解决linq表达式拼接动态查询的类PredicateBuilder
2011-12-21 11:05
519 查看
解决linq表达式拼接动态查询的类PredicateBuilder 在domain service 的客户端调用时应用 public static class PredicateBuilder { /// <summary> /// 机关函数应用True时:单个AND有效,多个AND有效;单个OR无效,多个OR无效;混应时写在AND后的OR有效 /// 机关函数应用False时:单个AND无效,多个AND无效;单个OR有效,多个OR有效;混应时写在OR后面的AND有效 /// </summary> public static Expression<Func<T, bool>> True<T>() { return f => true; } public static Expression<Func<T, bool>> False<T>() { return f => false; } public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); return Expression.Lambda<Func<T, bool>> (Expression.Or(expr1.Body, invokedExpr), expr1.Parameters); } public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1, Expression<Func<T, bool>> expr2) { var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>()); return Expression.Lambda<Func<T, bool>> (Expression.And(expr1.Body, invokedExpr), expr1.Parameters); } public static Predicate<T> ConvertToPredicate<T>(this Func<T, bool> func) { return new Predicate<T>(func); } } 社会主义制度的建立给我们开辟了一条到达理想境界的道路,而理想境界的实现还要靠我们的辛勤劳动。——毛泽东
终于解决了Linq动态、多条件在EF、RIA、DS里面的用法,整理了相关参考地址。
PredicateBuilder(Expressions扩展)
http://www.albahari.com/nutshell/linqkit.aspx
System.Linq.Dynamic (Dynamic.cs)扩展类
可以在VS自带demo里找到。
(VS2010目录C:\Program Files (x86)\Microsoft Visual Studio 10.0\Samples\2052\CSharpSamples.zip-CSharpSamples\LinqSamples\DynamicQuery)
相关
动态模糊查询
http://stackoverflow.com/questions/4599989/how-dynamic-library-system-linq-dynamic-support-like-operator
动态查询语法
http://www.beansoftware.com/ASP.NET-Tutorials/Dynamic-LINQ.aspx
动态创建在WCF RIA Services
http://weblogs.asp.net/fredriknormen/archive/2009/12/30/wcf-ria-services-dynamically-create-a-criteria-on-the-client-side.aspx
其他
Writing Dynamic Linq Queries in Linq-to-Entities
http://naspinski.net/post/Writing-Dynamic-Linq-Queries-in-Linq-to-Entities.aspx
LINQ – LINQKit(PredicateBuilder)
http://www.dotblogs.com.tw/alonstar/archive/2010/06/30/16274.aspx
Expression<Func<T, bool>>相关,这东西我还没玩转,在DomainServices里不好使。
Building LINQ Queries at Runtime in C#
http://tomasp.net/blog/dynamic-linq-queries.aspx
linq to sql 多条件组合查询
http://www.cnblogs.com/ChaosHero/archive/2010/06/20/1761444.html
Linq动态查询与模糊查询(带源码示例)
http://www.cnblogs.com/killuakun/archive/2008/08/03/1259389.html 他用的PredicateBuilder,思路清晰,简单。
LINQ体验(17)——LINQ to SQL语句之动态查询
http://www.cnblogs.com/lyj/archive/2008/03/25/1122157.html
Linq To Sql进阶系列(六)用object的动态查询与保存log篇
http://www.cnblogs.com/126/archive/2007/09/09/887723.html
浅谈Linq To Sql集成数据库语言的优劣
http://www.bccn.net/Article/sjk/sqlserver/jszl/200709/6578.html
贴个例子,基本解决了string、int、datetime、bool类型的拼接方法,特别注意下如果拼成一条string的格式写法,用参数传递格式就不太重要了
MVVM-ViewModels private LoadOperation<V_Store> LoadSampleEntities() { this.CanLoad = false; string strWhere = " 1=1"; if (!string.IsNullOrWhiteSpace(Tt_Company)) strWhere += " and co_Company.Contains(\"" + Tt_Company + "\")"; if (!string.IsNullOrWhiteSpace(Tt_Name)) strWhere += " and co_Name.Contains(\"" + Tt_Name + "\")"; if (!string.IsNullOrWhiteSpace(Tt_MPa)) strWhere += " and co_MPa.Contains(\"" + Tt_MPa + "\")"; if (!string.IsNullOrWhiteSpace(Tt_Spec)) strWhere += " and co_Spec.Contains(\"" + Tt_Spec + "\")"; if (!string.IsNullOrWhiteSpace(Tt_Mill)) strWhere += " and co_Mill.Contains(\"" + Tt_Mill + "\")"; if (!string.IsNullOrWhiteSpace(Tt_Remark)) strWhere += " and co_Remark.Contains(\"" + Tt_Remark + "\")"; if (!string.IsNullOrWhiteSpace(Tt_StartAmount) && Utils.IsNumeric(Tt_StartAmount)) {//数量 Decimal SAmount = Decimal.Parse(Tt_StartAmount); strWhere += " and co_Amount >= " + SAmount; } if (!string.IsNullOrWhiteSpace(Tt_EndAmount) && Utils.IsNumeric(Tt_EndAmount)) { Decimal EAmount = Decimal.Parse(Tt_EndAmount); strWhere += " and co_Amount <= " + EAmount; } if (!string.IsNullOrWhiteSpace(Tt_StartWeight) && Utils.IsNumeric(Tt_StartWeight)) {//重量 Decimal SWeight = Decimal.Parse(Tt_StartWeight); strWhere += " and co_Weight >= " + SWeight; } if (!string.IsNullOrWhiteSpace(Tt_EndWeight) && Utils.IsNumeric(Tt_EndWeight)) { Decimal EWeight = Decimal.Parse(Tt_EndWeight); strWhere += " and co_Weight <= " + EWeight; } if (!string.IsNullOrWhiteSpace(Dt_StartTime)) {//时间 DateTime STime = DateTime.Parse(Dt_StartTime); string STimeFormat = STime.ToString("yyyy,MM,dd"); strWhere += " and co_UpDateTime >= DateTime(" + STimeFormat + ")"; } if (!string.IsNullOrWhiteSpace(Dt_EndTime)) { DateTime ETime = DateTime.Parse(Dt_EndTime).AddDays(1); string ETimeFormat = ETime.ToString("yyyy,MM,dd"); strWhere += " and co_UpDateTime <= DateTime(" + ETimeFormat + ")"; } if (Ci_IsZero) strWhere += " and co_Amount != 0 and co_Weight != 0"; //截入条件,返回一个查询后的Sum结果。 InvokeOperation<decimal> invokeOperation = ds.GetV_StoreWeightSum(strWhere, OnInvokeCallback, null); //载入了相同的条件,查询后返回记录。 return ds.Load(ds.GetV_StoreWhereQuery(strWhere).SortAndPageBy(_view)); } DomainService public IQueryable<V_Store> GetV_StoreWhere(string strWhere) { return ObjectContext.V_Store.Where(strWhere, new object[] { }).OrderByDescending(e => e.co_StoreID); } [Invoke] public decimal GetV_StoreWeightSum(string strWhere) { return ObjectContext.V_Store.Where(strWhere, new object[] { }).Sum(e => e.co_Weight); }
复制代码
解决linq表达式拼接动态查询的类PredicateBuilder
在domain service 的客户端调用时使用
public static class PredicateBuilder
{
/// <summary>
/// 构造函数使用True时:单个AND有效,多个AND有效;单个OR无效,多个OR无效;混合时写在AND后的OR有效
/// 构造函数使用False时:单个AND无效,多个AND无效;单个OR有效,多个OR有效;混合时写在OR后面的AND有效
/// </summary>
public static Expression<Func<T, bool>> True<T>() { return f => true; }
public static Expression<Func<T, bool>> False<T>() { return f => false; }
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.Or(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> expr1,
Expression<Func<T, bool>> expr2)
{
var invokedExpr = Expression.Invoke(expr2, expr1.Parameters.Cast<Expression>());
return Expression.Lambda<Func<T, bool>>
(Expression.And(expr1.Body, invokedExpr), expr1.Parameters);
}
public static Predicate<T> ConvertToPredicate<T>(this Func<T, bool> func)
{
return new Predicate<T>(func);
}
}
相关文章推荐
- 使用反射让linq实现动态查询, 类似拼接sql语句的where 条件
- 拼接linq查询表达式
- 拼接linq查询表达式
- 拼接LINQ动态表达式
- Linq to Entity 动态拼接查询条件(重点是OR)
- Linq动态查询简易解决之道(原创)
- 【整理】Linq to Entity 动态拼接查询条件(重点是OR)
- Linq从两个edmx(两个数据库)中选取数据进行查找出现错误解决 (指定的 LINQ 表达式包含对与不同上下文关联的查询)
- linq 动态拼接查询条件 扩展方法
- 使用反射让linq实现动态查询, 类似拼接sql语句的where 条件
- 用PredicateBuilder实现Linq动态拼接查询
- linq 动态拼接查询条件 扩展方法
- 动态拼接LINQ查询条件的解决方案
- solr 通过【配置、多值字段、动态字段】来解决文本表达式查询精确到句子的问题20171214
- linq里lambda写的join查询,并附加动态拼接的条件,条件为enum类型的查询
- Linq 动态表达式 多条件查询
- linq 实现查询字符串拼接 : And 和 OR 两种方式
- LINQ 查询表达式
- linq - 动态生成查询
- C# linq查询 动态OrderBy