技术背景:框架MVC,linq to Entity 需要一定的lambda书写能力
问题:在简单的orm中完成一些简单的增删查改是通过where insert delete update 完成的,但是在这个过程中出现了一个需求:多项条件的and 和or 的组合查询
众所周知直接通过linq 写的话很方便,但是我们的框架使用了linq to entity,如果只用lambda来写比较复杂的and 和or 查询就比较麻烦了。
一:简单and 和or 查询
public void TextFoZK()
{
using (var dbContext = new CRMDbContext())
{
//第一句解析出来的sql是 select * from membertype where commercialtenantid=0 or name='住客'
dbContext.MemberType.Where(m => m.CommercialTenantID == || m.Name == "住客");
//第二句解析出来的sql是 select * from membertype where commercialtenantid=0 and name='住客'
dbContext.MemberType.Where(m => m.CommercialTenantID == && m.Name == "住客");
}
}
二:复杂逻辑的and 和or 查询
public void TextFoZK(int status, string name, int commercialtenantid)
{
using (var dbContext = new CRMDbContext())
{
IQueryable<MemberType> iqm = dbContext.MemberType;
if (status > )
{
iqm = iqm.Where(m => m.Status == status);
}
if (!string.IsNullOrEmpty(name))
{
iqm = iqm.Where(m => m.Name == name && m.CommercialTenantID == commercialtenantid);
}
iqm = iqm.Where(m => m.ID > || m.ID == );
iqm.ToList();
//select * from membertype where (status=1) and (name='住客' and commercialtenantid=1) and (id>0 or id=1)
}
}
这里使用了IQuerable的扩展方法where ,代表着每个iquerable之间为and 关系,但是又可以包含or
三:复杂and 和 or
public void TextFoZK(int status, string name, int commercialtenantid)
{
using (var dbContext = new CRMDbContext())
{
IQueryable<MemberType> iqm = dbContext.MemberType;
if (status > )
{
iqm = iqm.Where(m => m.Status == status);
}
if (!string.IsNullOrEmpty(name))
{
iqm = iqm.Where(m => m.Name == name && m.CommercialTenantID == commercialtenantid);
}
//重新声明一个iq,两个iq 之间为or 关系
IQueryable<MemberType> iqmtwo = dbContext.MemberType;
iqmtwo = iqmtwo.Where(m => m.ID > || m.ID == );
iqm = iqm.Union(iqmtwo);
iqm.ToList(); }
}
这里使用了iquerable中的扩展方法union 可以把多个iq方法合成为一个iq ,之间为union all 关系
第一个iq 为一个结果集,第二个为一个结果集,最后合并两个结果集。
可以满足一个sql过程中查询多处结果的要求,但是生成的sql还是有点麻烦
exec sp_executesql N'SELECT TOP (10)
[Project4].[C1] AS [C1],
[Project4].[C2] AS [C2],
[Project4].[C3] AS [C3],
[Project4].[C4] AS [C4],
[Project4].[C5] AS [C5],
[Project4].[C6] AS [C6],
[Project4].[C7] AS [C7],
[Project4].[C8] AS [C8],
[Project4].[C9] AS [C9],
[Project4].[C10] AS [C10]
FROM ( SELECT [Project4].[C1] AS [C1], [Project4].[C2] AS [C2], [Project4].[C3] AS [C3], [Project4].[C4] AS [C4], [Project4].[C5] AS [C5], [Project4].[C6] AS [C6], [Project4].[C7] AS [C7], [Project4].[C8] AS [C8], [Project4].[C9] AS [C9], [Project4].[C10] AS [C10], row_number() OVER (ORDER BY [Project4].[C1] ASC) AS [row_number]
FROM ( SELECT
[Distinct1].[C1] AS [C1],
[Distinct1].[C2] AS [C2],
[Distinct1].[C3] AS [C3],
[Distinct1].[C4] AS [C4],
[Distinct1].[C5] AS [C5],
[Distinct1].[C6] AS [C6],
[Distinct1].[C7] AS [C7],
[Distinct1].[C8] AS [C8],
[Distinct1].[C9] AS [C9],
[Distinct1].[C10] AS [C10]
FROM ( SELECT DISTINCT
[UnionAll1].[ID] AS [C1],
[UnionAll1].[CommercialTenantID] AS [C2],
[UnionAll1].[Name] AS [C3],
[UnionAll1].[Status] AS [C4],
[UnionAll1].[Discount] AS [C5],
[UnionAll1].[GiveIntegralScale] AS [C6],
[UnionAll1].[Creator] AS [C7],
[UnionAll1].[CreatorID] AS [C8],
[UnionAll1].[GMT_Create] AS [C9],
[UnionAll1].[GMT_Modified] AS [C10]
FROM (SELECT
[Extent1].[ID] AS [ID],
[Extent1].[CommercialTenantID] AS [CommercialTenantID],
[Extent1].[Name] AS [Name],
[Extent1].[Status] AS [Status],
[Extent1].[Discount] AS [Discount],
[Extent1].[GiveIntegralScale] AS [GiveIntegralScale],
[Extent1].[Creator] AS [Creator],
[Extent1].[CreatorID] AS [CreatorID],
[Extent1].[GMT_Create] AS [GMT_Create],
[Extent1].[GMT_Modified] AS [GMT_Modified]
FROM [dbo].[commercialtenant_membertype] AS [Extent1]
WHERE [Extent1].[CommercialTenantID] = @p__linq__0
UNION ALL
SELECT
[Extent2].[ID] AS [ID],
[Extent2].[CommercialTenantID] AS [CommercialTenantID],
[Extent2].[Name] AS [Name],
[Extent2].[Status] AS [Status],
[Extent2].[Discount] AS [Discount],
[Extent2].[GiveIntegralScale] AS [GiveIntegralScale],
[Extent2].[Creator] AS [Creator],
[Extent2].[CreatorID] AS [CreatorID],
[Extent2].[GMT_Create] AS [GMT_Create],
[Extent2].[GMT_Modified] AS [GMT_Modified]
FROM [dbo].[commercialtenant_membertype] AS [Extent2]
WHERE (0 = [Extent2].[CommercialTenantID]) AND (N''住客'' = [Extent2].[Name])) AS [UnionAll1]
) AS [Distinct1]
) AS [Project4]
) AS [Project4]
WHERE [Project4].[row_number] > 0
ORDER BY [Project4].[C1] ASC',N'@p__linq__0 int',@p__linq__0=1
sql 想看的可以打开看看
最后提供一种扩展方法
四:多条件之间均为or
/// <summary>
/// 传入条件之间为OR查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="predicates"></param>
/// <returns></returns>
public static IQueryable<T> WhereOR<T>(this IQueryable<T> source, params Expression<Func<T, bool>>[] predicates)
{
if (source == null) throw new ArgumentNullException("source");
if (predicates == null) throw new ArgumentNullException("predicates");
if (predicates.Length == 0) return source.Where(x => false); // no matches!
if (predicates.Length == 1) return source.Where(predicates[]); // simple var param = Expression.Parameter(typeof(T), "x");
Expression body = Expression.Invoke(predicates[], param);
for (int i = 1; i < predicates.Length; i++)
{
body = Expression.OrElse(body, Expression.Invoke(predicates[i], param));
}
var lambda = Expression.Lambda<Func<T, bool>>(body, param);
return source.Where(lambda);
}
public void TextFoZK(int status, string name, int commercialtenantid)
{
using (var dbContext = new CRMDbContext())
{
IQueryable<MemberType> iqm = dbContext.MemberType;
if (status > 0)
{
iqm = iqm.Where(m => m.Status == status);
}
if (!string.IsNullOrEmpty(name))
{
iqm = iqm.Where(m => m.Name == name && m.CommercialTenantID == commercialtenantid);
}
var predicates = new List<Expression<Func<MemberType, bool>>>();
predicates.Add(m => m.CommercialTenantID == 0 && m.Name == "住客");
predicates.Add(m=>m.ID>0);
//这两个条件之间为or
//与iqm之间为and
//如果要与iqm之间为or 也可以使用union方法,但是总感觉有点麻烦
iqm = iqm.WhereOR(predicates.ToArray());
iqm.ToList();
//select * from membertype where (status=1) and (name='住客' and commercialtenantid=1) or (id>0 or id=1)
}
}
我感觉已经研究到这一步了索性就再往深的看一看,于是我找到了IQuerable的where 和union 的底层方法
public static IQueryable<TSource> Union<TSource>(this IQueryable<TSource> source1, IEnumerable<TSource> source2)
{
if (source1 == null)
{
throw System.Linq.Error.ArgumentNull("source1");
}
if (source2 == null)
{
throw System.Linq.Error.ArgumentNull("source2");
}
return source1.Provider.CreateQuery<TSource>(Expression.Call(null, ((MethodInfo) MethodBase.GetCurrentMethod()).MakeGenericMethod(new Type[] { typeof(TSource) }), new Expression[] { source1.Expression, GetSourceExpression<TSource>(source2) }));
}
[__DynamicallyInvokable]
public static IQueryable<TSource> Where<TSource>(this IQueryable<TSource> source, Expression<Func<TSource, bool>> predicate)
{
if (source == null)
{
throw System.Linq.Error.ArgumentNull("source");
}
if (predicate == null)
{
throw System.Linq.Error.ArgumentNull("predicate");
}
return source.Provider.CreateQuery<TSource>(Expression.Call(null, ((MethodInfo) MethodBase.GetCurrentMethod()).MakeGenericMethod(new Type[] { typeof(TSource) }), new Expression[] { source.Expression, Expression.Quote(predicate) }));
}
只是浅薄只能看到这一步。
最后我还求助大神,大神又提出一个方案使用的是expresion方法,这个其实就是我上面提供的whereor 方法内的同样技术,只不过我是封装了的。
Expression<Func<MemberType, bool>> funtyps = c => c.ID > 0;
Expression<Func<MemberType, bool>> ortype = c => c.CommercialTenantID == 0 && c.Name == "住客";
funtyps = funtyps.Or(ortype);
iqmemebertype = iqmemebertype.Where(funtyps);
应该是是要更好的方案,我只是记录我目前理解的方法。
最后附上关于expression的底层方法or 和 and ,提供了express语句之间可或与查询的接口
/// <summary>
/// 用于多条件动态查询
/// zk(-_-)
/// </summary>
public static class PredicateBuilderUtility
{
public static Expression<T> Compose<T>(this Expression<T> first, Expression<T> second, Func<Expression, Expression, Expression> merge)
{
// build parameter map (from parameters of second to parameters of first)
var map = first.Parameters.Select((f, i) => new { f, s = second.Parameters[i] }).ToDictionary(p => p.s, p => p.f);
// replace parameters in the second lambda expression with parameters from the first
var secondBody = ParameterRebinder.ReplaceParameters(map, second.Body);
// apply composition of lambda expression bodies to parameters from the first expression
return Expression.Lambda<T>(merge(first.Body, secondBody), first.Parameters);
}
/// <summary>
/// 动态And
/// </summary>
public static Expression<Func<T, bool>> And<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.AndAlso);
}
/// <summary>
/// 动态Or
/// </summary>
public static Expression<Func<T, bool>> Or<T>(this Expression<Func<T, bool>> first, Expression<Func<T, bool>> second)
{
return first.Compose(second, Expression.Or);
}
/// <summary>
/// 传入条件之间为OR查询
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="source"></param>
/// <param name="predicates"></param>
/// <returns></returns>
public static IQueryable<T> WhereOR<T>(this IQueryable<T> source, params Expression<Func<T, bool>>[] predicates)
{
if (source == null) throw new ArgumentNullException("source");
if (predicates == null) throw new ArgumentNullException("predicates");
if (predicates.Length == 0) return source.Where(x => false); // no matches!
if (predicates.Length == 1) return source.Where(predicates[]); // simple var param = Expression.Parameter(typeof(T), "x");
Expression body = Expression.Invoke(predicates[], param);
for (int i = 1; i < predicates.Length; i++)
{
body = Expression.OrElse(body, Expression.Invoke(predicates[i], param));
}
var lambda = Expression.Lambda<Func<T, bool>>(body, param);
return source.Where(lambda);
} }
expression
Linq to Entity 多条件 OR查询的更多相关文章
-
C# Linq to Entity 多条件 OR查询
技术背景:框架MVC,linq to Entity 需要一定的lambda书写能力 问题:在简单的orm中完成一些简单的增删查改是通过where insert delete update 完成的,但是 ...
-
Linq to Entity 时间差作为筛选条件产生的问题
前言 在使用 Linq to Entity 的時候,會把之前 Linq to SQL 的想法就帶進去,寫好之後編譯也都不會出錯,但是實際上在跑的時候就會出現錯誤訊息了,這點真的要注意了.這次我遇到問題 ...
-
Linq to Entity经验:表达式转换
http://www.cnblogs.com/ASPNET2008/archive/2012/10/27/2742434.html 最近一年的项目,我主要负责一些小型项目(就是指企业内部的小项目),在 ...
-
LINQ之路 8: 解释查询(Interpreted Queries)
LINQ提供了两个平行的架构:针对本地对象集合的本地查询(local queries),以及针对远程数据源的解释查询(Interpreted queries). 在讨论LINQ to SQL等具体技术 ...
-
LINQ之路 7:子查询、创建策略和数据转换
在前面的系列中,我们已经讨论了LINQ简单查询的大部分特性,了解了LINQ的支持计术和语法形式.至此,我们应该可以创建出大部分相对简单的LINQ查询.在本篇中,除了对前面的知识做个简单的总结,还会介绍 ...
-
asp.net mvc多条件+分页查询解决方案
开发环境vs2010 css:bootstrap js:jquery bootstrap paginator 原先只是想做个mvc的分页,但是一般的数据展现都需要检索条件,而且是多个条件,所以就变成了 ...
-
EF架构~在Linq to Entity中使用日期函數
回到目录 眾所周知,在linq to entity的查询语句中,不允许出现ef不能识别的关键字,如Trim,Substring,TotalDays等.net里的关键字,在EF查询里都是不被支持的,它的 ...
-
关于Entity Framework自动关联查询与自动关联更新导航属性对应的实体注意事项说明
一.首先了解下Entity Framework 自动关联查询: Entity Framework 自动关联查询,有三种方法:Lazy Loading(延迟加载),Eager Loading(预先加载) ...
-
Linq to Entity中连接两个数据库时要注意的问题
Linq to Entity中连接两个数据库时要注意的问题 今天大学同学问了我一个问题,Linq to Entity中连接两个数据库时,报错“指定的 LINQ 表达式包含对与不同上下文关联的查询的引用 ...
随机推荐
-
OpenCV播放视频带滚动条(3)
演示 :一个带有滚动条的播放视频的代码. #include "stdafx.h" #include <opencv2/core/core.hpp> #inclu ...
-
Windows性能查看器:系统的性能信息(I/O,IIS最大连接数,Sql) ,以及解决 asp.net IIS 一二百多用户并发
在测试过程中,我们经常需要知道“系统的资源利用情况”来监测我们的测试执行情况,来查看测试环境是否有效,测试结果是否可信,或者是在无人值守时保存结果,等我们值班时再来分析. 1.在Windows环境下, ...
-
10、面向对象以及winform的简单运用(isMdicontainer的设置、timer控件进行倒计时的制作)
IsMdicontainer的设置 这是对于整个窗体的设置,将一个窗体的IsMdicontainer设置为true之后,再打开新窗体便可以让新窗体被父容器包括在内. 操作方法: 1)先建立一个子窗体C ...
-
Headfirst设计模式的C++实现——迭代器(Iterator)
iterator.h #ifndef _ITERATOR_H_ #define _ITERATOR_H_ #include "menu_item.h" class Iterator ...
-
MapReduce多表连接
多表关联 多表关联和单表关联类似,它也是通过对原始数据进行一定的处理,从其中挖掘出关心的信息.下面进入这个实例. 1 实例描述 输入是两个文件,一个代表工厂表,包含工厂名列和地址编号列:另一个代表地址 ...
-
HDOJ/HDU 1075 What Are You Talking About(字符串查找翻译~Map)
Problem Description Ignatius is so lucky that he met a Martian yesterday. But he didn't know the lan ...
-
jQuery1.9(辅助函数)学习之—— jQuery.param( obj ); 编辑
jQuery.param( obj ); 返回一个String 描述: 创建一个数组或对象序列化的的字符串,适用于一个URL 地址查询字符串或Ajax请求. jQuery.param( obj ); ...
-
修改IIS虚拟目录名称
@echo off echo ------------------------------------------------------------------------------ echo - ...
-
5 Http请求中文乱码处理
java 乱码分很多种,这里主要研究解决http请求中出现乱码的情况. http请求出现中文乱码的主要原因:发送方与接收方编码不一致,服务器默认支持的编码与web应用不一致,如:tomcat 是国外程 ...
-
[BJWC2010] 严格次小生成树
[BJWC2010]严格次小生成树算法及模板 所谓次小生成树,即边权之和第二小的生成树,但所谓严格,就是不能和最小的那个相等. 求解严格次小生成树的方法一般有倍增和LCT两种.当然LCT那么高级的我当 ...