[C#] Linq 动态条件查询
应用背景:以货品为例,在基础数据中配置货品的判断规则,要根据这个规则筛选出符合条件的集合。
创建货品类
public class Product
{
public string Name { get; set; }
public string Code { get; set; }
public string Unit { get; set; }
}
主要实现方法
public void GetProducts()
{
#region 创建List 实际应用从数据库中取值
var products = new[] {
new {Name = "货品1", Code = "001", Unit = "个"},
new {Name = "货品2", Code = "002", Unit = "件"},
new {Name = "货品3", Code = "003", Unit = "瓶"},
new {Name = "货品4", Code = "004", Unit = "个"},
new {Name = "货品1", Code = "005", Unit = "台"},
};
List<Product> lsProducts = new List<Product>();
foreach (var q in products)
{
Product product = new Product();
product.Name = q.Name;
product.Code = q.Code;
product.Unit = q.Unit;
lsProducts.Add(product);
}
#endregion
//获取到的判断规则 判断规则为Code和Unit 其中Name为固定条件
string condition = "Code,Unit";
string[] arrayCondition = condition.Split(',');
//查询结果
List<Product> result = lsProducts.Where(a => Filter(a, arrayCondition, "货品1", "005", "")).ToList(); //0条
//只需判断Code 其中Name为固定条件
string condition2 = "Code";
arrayCondition = condition2.Split(',');
result = lsProducts.Where(a => Filter(a, arrayCondition, "货品1", "005", "")).ToList();//1条
//没有判断规则 其中Name为固定条件
string condition3 = "";
arrayCondition = condition3.Split(',');
result = lsProducts.Where(a => Filter(a, arrayCondition, "货品1", "005", "")).ToList();//2条
} public bool Filter(Product product, string[] arrayCondition, string name, string code, string unti)
{
bool result = false;
result = (product.Name == name)
&& (!arrayCondition.Contains("Code") ? true : product.Code == code)
&& (!arrayCondition.Contains("Unit") ? true : product.Unit == unti);
return result;
}
[C#] Linq 动态条件查询的更多相关文章
- 【Oracle】曾经的Oracle学习笔记(4-7)多表联合查询,子查询,动态条件查询
一.多表联合查询 二.子查询 三.动态条件查询 LESSON 4 Displaying Data from Multiple Tables------------------------------- ...
- mybatis+maven+父子多模块进行crud以及动态条件查询
使用IDEA创建maven项目,File→New→Project→maven→Next→填写GroupId(例:com.zyl)和ArtifactId(mybatis-demo-parent)→Nex ...
- Linq动态条件
很多情况下,我们开发程序,需要动态拼接SQL查询语句; 比如 select top 1 * from User where age= 18 and name = 'renruiquan' 其中红 ...
- linq 多条件查询
Linq 进行多条件查询的时候使用PredicateBuilder帮助类可以很好的解决. 类的源码: public static class PredicateBuilder { /// <su ...
- Spring Data JPA,一种动态条件查询的写法
我们在使用SpringData JPA框架时,进行条件查询,如果是固定条件的查询,我们可以使用符合框架规则的自定义方法以及@Query注解实现. 如果是查询条件是动态的,框架也提供了查询接口. Jpa ...
- Linq in条件查询
Linq 实现sql中的not in和in条件查询 T-SQL的IN: Select ProductID, ProductName, CategoryID From dbo.Products Wh ...
- spring-data-jpa动态条件查询
//获取动态条件的集合List<Long> list = new ArrayList<Long>(); Long sysUserId = currentUser.getSysU ...
- 用PredicateBuilder实现Linq动态拼接查询
在使用Linq查询的时候,特别是如果你在使用Entiry Framwork,有时会遇到动态查询的情况(客户的查询条件是不固定的拼接查询).我们能想到的第一方案应该是拼接SQL,的确这样是可以达到我们的 ...
- SpringBoot中Example的动态条件查询
一.无匹配器的情况: Person person = new Person(); person.setName("test"); Role role = new Role(); r ...
随机推荐
- UVa 489 Hangman Judge(字符串)
Hangman Judge In ``Hangman Judge,'' you are to write a program that judges a series of Hangman gam ...
- LeetCode 709. To Lower Case (转换成小写字母)
题目标签:String 题目让我们把大写字母转换成小写,只要遇到的是大写字母,把它 + 32 变成 小写就可以了. Java Solution: Runtime beats 100.00% 完成日期: ...
- STM32跑马灯
#include "stm32f10x.h" #include "led.h" #include "delay.h" #include &q ...
- const与static类成员
const成员函数作用1. 不能修改类的成员变量,同时不能调用类的非const成员函数.(const成员函数中,this的类型是一个指向const类类型对象的const指针,const成员函数返回*t ...
- Linux Framebuffer驱动剖析之中的一个—软件需求
嵌入式企鹅圈将以本文作为2015年的终结篇,以回应第一篇<Linux字符设备驱动剖析>.嵌入式企鹅圈一直专注于嵌入式Linux和物联网IOT双方面的原创技术分享,稍后会公布嵌入式企鹅圈的2 ...
- Codeforces--633D--Fibonacci-ish (map+去重)(twice)
Fibonacci-ish Time Limit: 3000MS Memory Limit: 524288KB 64bit IO Format: %I64d & %I64u Su ...
- bzoj1877 晨跑(费用流)
1877: [SDOI2009]晨跑 Time Limit: 4 Sec Memory Limit: 64 MBSubmit: 2138 Solved: 1145 Description Elax ...
- 安卓UI自适应性
出于安卓的碎片化原因,针对不同屏幕大小,最好是做到以下几点: 1.能用相对布局的就不用绝对布局,尽量使用权重,weight设置,相对布局和线性布局同条件情况下,优先选线性布局 2.在res目录下创建不 ...
- PHP富文本编辑器 之Kindeditor的使用 一
一.下载编辑器源码 KindEditor 4.1.10 (2013-11-23) [1143KB] 下载页面: http://kindeditor.net/down.php 二.部署编辑器 将下载文件 ...
- 怎么在windows上安装 ansible How to install ansible to my python at Windows
答案是不能再window上安装,答案如下: It's back! Take the 2018 Developer Survey today » Join Stack Overflow to learn ...