linq里lambda写的join查询,并附加动态拼接的条件,条件为enum类型的查询
因为查询条件不固定的原因,sql式的linq查询没法动态拼接条件。
网上搜的资料整理之后终于解决。
参考资料:
enum使用 http://blog.csdn.net/slowlifes/article/details/7799444
linq动态查询 http://msdn.microsoft.com/zh-cn/subscriptions/dd470085.aspx
lambda join使用 http://blog.csdn.net/lai_gb/article/details/4491843
string whereParams = GetWhereParams(SolutionGroup, CapabilityType);
var query = crm.new_usercapabilitynew_usercapabilities.Where(uc => uc.new_user == SystemUserId).
Join(
crm.new_capabilitynew_capabilities,
uc => uc.new_capabilityid,
c => c.new_capabilityid,
(uc, c) => new
{
uc,
c
}
).Where(o => o.c.new_name.Contains(Capability)).Where(whereParams)
.OrderBy(o => o.uc.new_capabilityidLabel)
.Select(o => new
{
new_capabilityid = o.uc.new_capabilityid,
new_usercapabilityid = o.uc.new_usercapabilityid,
new_capabilityidLabel = o.uc.new_capabilityidLabel,
new_developmentarea = o.uc.new_developmentarea,
new_rating = o.uc.new_rating
});
public string GetWhereParams(string SolutionGroup, string CapabilityType)
{
var enumgroup = Enum.GetNames(typeof(Xrm.new_capability.NewSolutiongroup)).Where(e => e.ToLower().Contains(SolutionGroup.ToLower())).ToList();
string groupLinqStr = "";
if (enumgroup.Count > )
{
groupLinqStr += "(";
for (int i = ; i < enumgroup.Count; i++)
{
Xrm.new_capability.NewSolutiongroup enumitem = (Xrm.new_capability.NewSolutiongroup)Enum.Parse(typeof(Xrm.new_capability.NewSolutiongroup), enumgroup[i]);
groupLinqStr += "c.new_solutiongroup = " + ((int)enumitem).ToString();
if (i != enumgroup.Count - )
{
groupLinqStr += " or ";
}
}
groupLinqStr += ")";
}
else if (!string.IsNullOrEmpty(SolutionGroup))
{
groupLinqStr += "c.new_solutiongroup = 0";
} var enumtype = Enum.GetNames(typeof(Xrm.new_capability.NewCapabilitytype)).Where(e => e.ToLower().Contains(CapabilityType.ToLower())).ToList();
if (enumtype.Count > )
{
if (groupLinqStr.Length > ) groupLinqStr += " and ";
groupLinqStr += "(";
for (int i = ; i < enumtype.Count; i++)
{
Xrm.new_capability.NewCapabilitytype enumitem = (Xrm.new_capability.NewCapabilitytype)Enum.Parse(typeof(Xrm.new_capability.NewCapabilitytype), enumtype[i]);
groupLinqStr += "c.new_capabilitytype = " + ((int)enumitem).ToString();
if (i != enumtype.Count - )
{
groupLinqStr += " or ";
}
}
groupLinqStr += ")";
}
else if (!string.IsNullOrEmpty(CapabilityType))
{
if (groupLinqStr.Length > ) groupLinqStr += " and ";
groupLinqStr += "c.new_capabilitytype = 0";
}
return groupLinqStr;
}
linq里lambda写的join查询,并附加动态拼接的条件,条件为enum类型的查询的更多相关文章
- SqlServer存储过程应用二:分页查询数据并动态拼接where条件
前言 开发中查询功能是贯穿全文的,我们来盘一盘使用存储过程分页查询,并且支持动态拼接where条件. 划重点:支持动态拼接where条件 对存储过程的使用有疑问的同学去[SqlServer存储过程的创 ...
- .NetCore 使用 Linq 动态拼接Expression表达式条件来实现 对EF、EF Core 扩展查询排序操作
相信在使用EF的时候对查询条件或者排序上的处理令人心烦,下面我们就来动态拼接表达式解决这一问题 当我们在查询中使用Where的时候可以看到如下参数 下面我们就来扩展 Expression<Fun ...
- 使用mybatis提供的各种标签方法实现动态拼接Sql。使用foreach标签实现遍历查询。比如实现select * from user where id in(1,10,24)这条sql查询语句。
向sql传递数组或List,mybatis使用foreach解析,如下: 需求: 传入多个id查询用户信息,用下边的sql实现: select * from user where id in(1,10 ...
- 在MyBatis中查询数据、涉及多参数的数据访问操作、插入数据时获取数据自增长的id、关联表查询操作、动态SQL、关于配置MyBatis映射没有代码提示的解决方案
1. 单元测试 在单元测试中,每个测试方法都需要执行相同的前置代码和后置代码,则可以自定义2个方法,分别在这2个方法中执行前置代码和后置代码,并为这2个方法添加@Before和@After注解,然后, ...
- sql 、linq、lambda 查询语句的区别
LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量中被查询的值 [group by 条件] Lambda ...
- 浅谈sql 、linq、lambda 查询语句的区别
浅谈sql .linq.lambda 查询语句的区别 LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量 ...
- linq与lambda 常用查询语句写法对比
LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量中被查询的值 [group by 条件] Lambda ...
- Linq基础+Lambda表达式对数据库的增删改及简单查询
一.Linq to sql 类 高集成化的数据库访问技术 使用Linq可以代替之前的Ado.Net,省去了自己敲代码的实体类和数据访问类的大量工作 实体类: 添加一个Linq to sql 类 --- ...
- LINQ to SQL语句之Join和Order By
Join操作 适用场景:在我们表关系中有一对一关系,一对多关系,多对多关系等.对各个表之间的关系,就用这些实现对多个表的操作. 说明:在Join操作中,分别为Join(Join查询), SelectM ...
随机推荐
- 使用TimeSpan对象获取时间间隔
实现效果: 关键知识: TimeSpan对象表是时间间隔或持续时间,两个DateTime对象相减,则会得到一个TimeSpan对象 使用其days ,hours,minutes等属性 实现代码: pr ...
- 【洛谷P1966】火柴排队
火柴排队 题目链接 ∑(ai−bi)^2=∑ai^2-2*∑ai*bi+∑bi^2 显然∑ai^2+∑bi^2是不变的,我们要让 2*∑ai*bi最大,才能使原式最小 然后我们一眼就可以看出来, ...
- javascript ActiveXObject FileSystemObject 对象,创建、复制、删除、读取文件等
Javascript是网页制作中离不开的脚本语言,依靠它,一个网页的内容才生动活泼.富有朝气.但也许你还没有发现并应用它的一些更高级的功能吧?比如,对文件和文件夹进行读.写和删除,就象在VB.VC等高 ...
- 【题解】洛谷P2532 [AHOI2012]树屋阶梯(卡特兰数+高精)
洛谷P2532:https://www.luogu.org/problemnew/show/P2532 思路 来自Sooke大佬的推导: https://www.luogu.org/blog/Sook ...
- 简述 private、 protected、 public、 internal 修饰符的访问权限
简述 private. protected. public. internal 修饰符的访问权限. private : 私有成员, 在该类的内部才可以访问. protected : 保护成员,该类内部 ...
- Struts2知识点小结(四)--拦截器与注解开发
一.Struts2的拦截器(interceptor) 作用:当请求进入struts2框架后(进入之前可以用filter进行拦截),想对请求进行拦截操作(功能增强.权限控制),需要拦截器组件 1.str ...
- shell的命令格式
参考高峻峰 著 循序渐进Linux(第二版) command [options] [arguments] command:表示命令的名称 options:表示命令的选项 arguments:表示命令的 ...
- python3.5怎么打包编译
问题:用Python开发的小工具有时需要编译打包为Windows(*.exe).Mac等操作系统下的可执行性文件以供非程序员使用. 解决方案: 一.py2exe 目前只支持到Python3.4,暂不支 ...
- linux后台程序开发常用工具
linux开发工具: 1.编辑工具:1)sourceInsight2)Notepad++3)UltraEdit4)Altova XMLSpy 2.linux服务器访问工具:1)FileZilla2)X ...
- JSP/Servlet开发——第六章 JSP开发业务应用
1. 大容量的数据显示的缺点: ●当数据量较多时,用户需要拖动页面才能浏览更多信息: ●数据定位不便: 2.分页显示: ●既能显示多条数据,又不需要拖动页面,是数据更加清晰直观,页面不再冗长,也不受数 ...