因为查询条件不固定的原因,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类型的查询的更多相关文章

  1. SqlServer存储过程应用二:分页查询数据并动态拼接where条件

    前言 开发中查询功能是贯穿全文的,我们来盘一盘使用存储过程分页查询,并且支持动态拼接where条件. 划重点:支持动态拼接where条件 对存储过程的使用有疑问的同学去[SqlServer存储过程的创 ...

  2. .NetCore 使用 Linq 动态拼接Expression表达式条件来实现 对EF、EF Core 扩展查询排序操作

    相信在使用EF的时候对查询条件或者排序上的处理令人心烦,下面我们就来动态拼接表达式解决这一问题 当我们在查询中使用Where的时候可以看到如下参数 下面我们就来扩展 Expression<Fun ...

  3. 使用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 ...

  4. 在MyBatis中查询数据、涉及多参数的数据访问操作、插入数据时获取数据自增长的id、关联表查询操作、动态SQL、关于配置MyBatis映射没有代码提示的解决方案

    1. 单元测试 在单元测试中,每个测试方法都需要执行相同的前置代码和后置代码,则可以自定义2个方法,分别在这2个方法中执行前置代码和后置代码,并为这2个方法添加@Before和@After注解,然后, ...

  5. sql 、linq、lambda 查询语句的区别

    LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量中被查询的值 [group by 条件] Lambda ...

  6. 浅谈sql 、linq、lambda 查询语句的区别

    浅谈sql .linq.lambda 查询语句的区别 LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量 ...

  7. linq与lambda 常用查询语句写法对比

    LINQ的书写格式如下: from 临时变量 in 集合对象或数据库对象 where 条件表达式 [order by条件] select 临时变量中被查询的值 [group by 条件] Lambda ...

  8. Linq基础+Lambda表达式对数据库的增删改及简单查询

    一.Linq to sql 类 高集成化的数据库访问技术 使用Linq可以代替之前的Ado.Net,省去了自己敲代码的实体类和数据访问类的大量工作 实体类: 添加一个Linq to sql 类 --- ...

  9. LINQ to SQL语句之Join和Order By

    Join操作 适用场景:在我们表关系中有一对一关系,一对多关系,多对多关系等.对各个表之间的关系,就用这些实现对多个表的操作. 说明:在Join操作中,分别为Join(Join查询), SelectM ...

随机推荐

  1. 一步一步部署SSIS包图解教程

    本文就SQL统计分析SSIS包的部署进行一次详细的部署图解教程,Sql Server Integration Services 提供了非常简单的部署工具,利用这些工具可以方便地将包文件(*.dtsx) ...

  2. 促销规则 promotion rule

    参考:http://www.cnblogs.com/winstonyan/archive/2012/10/29/b2c_research_promotion_engine_and_rule_1.htm ...

  3. iOS10 开发权限适配设置 崩溃(上传打包后构建版本一直不显示)

    ios10 系统必须强制配置系统权限 如果不配置,调试的时候导致崩溃,还会引发包无效的问题,导致上传打包后构建版本一直不显示 解决方案1.在项目中找到info.plist文件,右键点击以 Source ...

  4. CF1042C Array Product(贪心,模拟)

    题目描述 You are given an array aa consisting of nn integers. You can perform the following operations w ...

  5. 魔板 Magic Squares(广搜,状态转化)

    题目背景 在成功地发明了魔方之后,鲁比克先生发明了它的二维版本,称作魔板.这是一张有8个大小相同的格子的魔板: 1 2 3 4 8 7 6 5 题目描述 我们知道魔板的每一个方格都有一种颜色.这8种颜 ...

  6. mysql——查询重复数据,及删除重复数据只保留一条数据

    查询 text 表中,user_name字段值重复的数据及重复次数 select user_name,count(*) as count from text 删除 text 表中,重复出现的数据只保留 ...

  7. /etc/fstab开机自动挂载设备配置

    第一列:设备名字(路径?) 第二列:设备挂载路径(挂载到的位置) 第三列:分区格式 第四列:文件系统参数(?) 第五列:是否自动dump备份 0   不要    1   定期    2  不定期 第六 ...

  8. centos7下双网卡绑定

    一.进入网卡配置目录 cd /etc/sysconfig/network-scripts 二.备份原有网卡 mv ifcfg-em* /tmp/ 三.配置双网卡 nmcli con add type ...

  9. css实现下拉菜单功能(多中实现方式即原理)

    引导思路: 1.需要用到的元素:position hover (z-index)  或(overflow)或(display)等等. 关键点就是div的溢出部分的处理. 2.实现过程: 2.1:就是要 ...

  10. jquery点击li 获取当前父节点所在类的索引

    jquery点击li 获取当前父节点所在类的索引 $('.jbcz').find('.content li').click(function(){ //alert($('.jbcz').find('. ...