entity framework 动态条件
entity framework 动态条件
问题:在实际编码过程中,根据不同的选择情况,会需要按照不同的条件查询数据集
如:状态confirmStatus ,如果为空的时候,查询全部,如果有具体值的时候,查询相应的值
同时还有其他条件,外键编号 task 等等。
这个时候,需要根据条件的值来判断是否增加该条件
using (CadalEntities cadalEntities = new CadalEntities())
{
string taskId = 12;
string itemConfirmStatus == "";
//构建动态条件数组
List<Expression<Func<V_TaskItem, bool>>> where = new List<Expression<Func<V_TaskItem, bool>>>();
//添加外键条件过滤
where.Add(x => x.task == taskId);
//判断是否需要添加其他条件
if (!string.IsNullOrEmpty(itemConfirmStatus))
{
where.Add(x => x.confirmStatus == itemConfirmStatus);
}
else
{
where.Add(x=>x.confirmStatus =="1" || x.confirmStatus =="3");
}
//定义查询 V_TaskItem查询的实体对象
IQueryable<V_TaskItem> query = null;
//加载查询条件
foreach (var w in where)
{
query= cadalEntities.V_TaskItem.Where(w);
}
//进行实际查询
int count = query.Count();
var list = query.OrderBy(x => x.id).Skip(requestStart).Take(requestLength).ToList();
}
结合网上众多例子,终于摸索出来了。
entity framework 动态条件的更多相关文章
- Entity Framework 动态构造Lambda表达式Expression<Func<T, bool>>
using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...
- Entity Framework 6 开发系列 目录
2014 年开始接触 Entity Framework 6 也快两年,用它已经沉淀了一个成熟架构,也用来开发了不少大大小小的产品和项目,直到这段时间,才真正有时间来回顾,重新学习它,为让大家更加了解E ...
- Entity Framework 6.1-Code First【转】
Entity Framework 6.1-Code First 分类: Entity Framework 2014-04-21 14:56 2034人阅读 评论(0) 收藏 举报 entityen ...
- 浅析Entity Framework Core2.0的日志记录与动态查询条件
前言 Entity Framework Core 2.0更新也已经有一段时间了,园子里也有不少的文章.. 本文主要是浅析一下Entity Framework Core2.0的日志记录与动态查询条件 去 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (34) ------ 第六章 继承与建模高级应用之多条件与QueryView
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 6-10 创建一个多条件过滤 问题 你想使用多个条件为实体过滤表中的行. 解决方案 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (35) ------ 第六章 继承与建模高级应用之TPH继承映射中使用复合条件
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 6-11 TPH继承映射中使用复合条件 问题 你想使用TPH为一张表建模,建模中使 ...
- 《Entity Framework 6 Recipes》中文翻译系列 (38) ------ 第七章 使用对象服务之动态创建连接字符串和从数据库读取模型
翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第七章 使用对象服务 本章篇幅适中,对真实应用中的常见问题提供了切实可行的解决方案. ...
- Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include
来自博客园开发团队开发前线最新消息: 在Entity Framework中,如果实体A关联了实体B,你想在加载实体A的同时加载实体B.通常做法是在LINQ查询中使用Include().但是,如果你在查 ...
- Entity Framework解决sql 条件拼接,完美解决 解决 不支持 LINQ 表达式节点类型“Invoke”【转】
传统的操作数据库方式,筛选数据需要用StringBuilder拼接一大堆的WHERE子句. 在Entity Framework中,代码稍有不慎就会造成巨大性能消耗,如: using(var db=ne ...
随机推荐
- Delphi 和 DFM
Delphi et les DFM Depuis la toute première version de Delphi, celui-ci intègre des fichiers à l'exte ...
- Linux下安装软件心得
1 软件安装方法: 源代码编译安装:tar.gz等压缩格式,需要经过手动编译,./configure,make ,make install ,然后进行配置操作 二进制安装:tar.gz等压缩格式,解压 ...
- bzoj2400
首先xor类的题目一定要逐位考虑,因为位位之间是不相互影响的逐位考虑每个点是0还是1,这就转化成了一个这样一个问题对于每个点可以选择属于S集合(这位是0)或T集合(这位是1)某些的点对(一条边的两端) ...
- HDOJ(HDU) 2148 Score(比较、)
Problem Description 转眼又到了一年的年末,Lele又一次迎来了期末考试.虽然说每年都要考试,不过今年的这场考试对Lele来说却意义重大. 因为经济原因,如果今年没有排在班级前几名, ...
- HDOJ 3466 Proud Merchants
Problem Description Recently, iSea went to an ancient country. For such a long time, it was the most ...
- How To Create a New User and Grant Permissions in MySQL
How to Create a New User Let’s start by making a new user within the MySQL shell: CREATE USER 'newus ...
- [Locked] Flatten 2D Vector
Problem Description: Implement an iterator to flatten a 2d vector. For example,Given 2d vector = [ [ ...
- SRM 396(1-250pt)
DIV1 250pt 题意:对于一个字符串s,若对于每一个i = 0 to s.size()-p-1都有s[i] = s[i+p]则称字符串s是p循环的."CATCATC", &q ...
- tessilstrona
Untitled Document
- 值得关注的 10 个 Python 英文博客
英文原文:http://pythontips.com/2013/07/31/10-python-blogs-worth-following/ 中文翻译参考: http://python.jobbole ...