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 ...
随机推荐
- POJ 2528 Mayor's posters 【区间离散化+线段树区间更新&&查询变形】
任意门:http://poj.org/problem?id=2528 Mayor's posters Time Limit: 1000MS Memory Limit: 65536K Total S ...
- static函数
C语言中使用静态函数的好处: 静态函数会被自动分配在一个一直使用的存储区,直到退出应用程序实例,避免了调用函数时压栈出栈,速度快很多. 关键字“static”,译成中文就是“静态 ...
- node学习----Promise 初见
今天在网上查看promise资料,发现promise有三种状态,pending,fullfilled,rejected.分别对应了初始化状态,成功状态及失败状态.为了理解写测试代码来实验:functi ...
- 菜鸟笔记 -- Chapter 11 格式化
我们在String中介绍过它有一个格式化的方法,在其它很多地方,也都能看到格式化的操作,那么这节我们就来认真了解一下Java中的格式化操作. 我们在操作中涉及到的格式化有字符串的格式化和一些其它数据类 ...
- nginx 方向代理
#nginx 监听原理 先监听端口 --> 再配置域名 -->匹配到就访问local 否则 没有匹配到域名就默认访问第一个监听端口的local地址 # vi nginx.conf user ...
- Rman 管理 archivelog 的命令
因为archivelog的相关信息是记录在controlfile中的,当物理删除后不会改变controlfile的设置.并且在查询相关的动态视图(例如v$archived_log)时,该部分日志仍然标 ...
- 【TOJ 3692】紧急援救
#include<iostream> #include<algorithm> #include<queue> using namespace std; #defin ...
- django+xadmin在线教育平台(十七)
8-1 课程列表 拷贝课程列表页到template目录 创建课程相关的urls.py Mxonline2/urls.py中声明包含到course的url中: # 课程app的url配置 url(r&q ...
- 邮件发送失败问题:Sending the email to the following server failed : smtp.qiye.163.com:25
[邮件发送错误] : Sending the email to the following server failed : smtp.qiye.163.com:25, {}org.apache.com ...
- aioysql(异步操作MySQL)-python
python异步IO初探 探索异步IO执之前,先说说IO的种类 阻塞IO最简单,即读写数据时,需要等待操作完成,才能继续执行.进阶的做法就是用多线程来处理需要IO的部分,缺点是开销会有些大. 非阻塞I ...