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 动态条件的更多相关文章

  1. Entity Framework 动态构造Lambda表达式Expression<Func<T, bool>>

    using System; using System.Collections.Generic; using System.Linq; using System.Linq.Expressions; us ...

  2. Entity Framework 6 开发系列 目录

    2014 年开始接触 Entity Framework 6 也快两年,用它已经沉淀了一个成熟架构,也用来开发了不少大大小小的产品和项目,直到这段时间,才真正有时间来回顾,重新学习它,为让大家更加了解E ...

  3. Entity Framework 6.1-Code First【转】

      Entity Framework 6.1-Code First 分类: Entity Framework 2014-04-21 14:56 2034人阅读 评论(0) 收藏 举报 entityen ...

  4. 浅析Entity Framework Core2.0的日志记录与动态查询条件

    前言 Entity Framework Core 2.0更新也已经有一段时间了,园子里也有不少的文章.. 本文主要是浅析一下Entity Framework Core2.0的日志记录与动态查询条件 去 ...

  5. 《Entity Framework 6 Recipes》中文翻译系列 (34) ------ 第六章 继承与建模高级应用之多条件与QueryView

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 6-10  创建一个多条件过滤 问题 你想使用多个条件为实体过滤表中的行. 解决方案 ...

  6. 《Entity Framework 6 Recipes》中文翻译系列 (35) ------ 第六章 继承与建模高级应用之TPH继承映射中使用复合条件

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 6-11  TPH继承映射中使用复合条件 问题 你想使用TPH为一张表建模,建模中使 ...

  7. 《Entity Framework 6 Recipes》中文翻译系列 (38) ------ 第七章 使用对象服务之动态创建连接字符串和从数据库读取模型

    翻译的初衷以及为什么选择<Entity Framework 6 Recipes>来学习,请看本系列开篇 第七章 使用对象服务 本章篇幅适中,对真实应用中的常见问题提供了切实可行的解决方案. ...

  8. Entity Framework 使用注意:Where查询条件中用到的关联实体不需要Include

    来自博客园开发团队开发前线最新消息: 在Entity Framework中,如果实体A关联了实体B,你想在加载实体A的同时加载实体B.通常做法是在LINQ查询中使用Include().但是,如果你在查 ...

  9. Entity Framework解决sql 条件拼接,完美解决 解决 不支持 LINQ 表达式节点类型“Invoke”【转】

    传统的操作数据库方式,筛选数据需要用StringBuilder拼接一大堆的WHERE子句. 在Entity Framework中,代码稍有不慎就会造成巨大性能消耗,如: using(var db=ne ...

随机推荐

  1. JPA概要

    1 JPA概述 JPA(Java Persistence API,Java持久化API),定义了对象-关系映射(ORM)以及实体对象持久化的标准接口. JPA是JSR-220(EJB3.0)规范的一部 ...

  2. sudo and su

    sudo bash : change the current user into root su u1: change the current user into u1 useradd: add ne ...

  3. AC+DP练习

    1.HDU 2222 Keywords Search 求目标串中出现了几个模式串. #include<iostream> #include<cstdio> #include&l ...

  4. 数据结构(Splay平衡树):HDU 1890 Robotic Sort

    Robotic Sort Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tota ...

  5. 后缀自动机(SAM):SPOJ Longest Common Substring II

    Longest Common Substring II Time Limit: 2000ms Memory Limit: 262144KB A string is finite sequence of ...

  6. 【贪心】Vijos P1615 旅行

    题目链接: https://vijos.org/p/1615 题目大意: N条路,路的高度给你,走一条路的耗费体力是从上一条路的高度到当前路高度的绝对值差. 可以改变一条路的高度,耗费的体力等于改变前 ...

  7. Search Insert Position——LeetCode

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  8. HDOJ 2016 数据的交换输出

    Problem Description 输入n(n<100)个数,找出其中最小的数,将它与最前面的数交换后输出这些数. Input 输入数据有多组,每组占一行,每行的开始是一个整数n,表示这个测 ...

  9. python编译环境发掘——从IDLE到sublime到pycharm到Anaconda

    一个好的编译器对于我们处理日常的科研很关键,好的编译器无论是从界面,字体风格,提示,调试等各方面都能从用户角度出发,提供最好的使用体验.Python本身自带的IDLE或者在CMD里进行操作和调试,对于 ...

  10. DFS hdu 1016

    http://acm.hdu.edu.cn/showproblem.php?pid=1016 #include <iostream> using namespace std; int a[ ...