[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 ...
随机推荐
- ios block 内存管理时使用注意
XMGStudent *stu = [[XMGStudent alloc] init]; __weak XMGStudent *weakStu = stu; stu.block = ^{ NSLog( ...
- ios--plist
// // main.m // 03-plist文件的回顾 // // Created by xiaomage on 15/12/29. // Copyright © 2015年 小码哥. All r ...
- What is a good buffer size for socket programming?
http://stackoverflow.com/questions/2811006/what-is-a-good-buffer-size-for-socket-programming 问题: We ...
- 【Bzoj2260】【Bzoj4349】商店购物 & 最小树形图
目录 List Bzoj 2260 商店购物 Description Input Output Sample Input Sample Output Bzoj 4349 最小树形图 Descripti ...
- [Codeforces 489E] Nastya and King-Shamans
[题目链接] http://codeforces.com/contest/992/problem/E [算法] 线段树 + 二分 时间复杂度 : O(NlogN^2) [代码] #include< ...
- oracle 分页方法
我分享两种: 1.用rownum select * from (select p.* , rownum rn from t_premium p where rn<= page * 10) a ...
- IBatis异常: Cannot find class: VARCHAR
今天再项目里添加新功能时,突然爆出 org.springframework.beans.factory.BeanCreationException: Error creating bean with ...
- Java中static final 与 final 的区别(转载)
转自:http://advance0683.iteye.com/blog/1107732 Java中static final 与 final 的区别: 例子: Java代码 import java.u ...
- Java的Thread.currentThread().getName() 和 this.getName() 以及 对象.getName()区别???
最近在看Java多线程这本书,但是发现里面有个概念自己搞不清楚.就是Thread.currentThread().getName() 和 this.getName() 以及 对象.getName()区 ...
- sqlyog注册码激活
姓 名(Name):ttrar 序 列 号(Code):8d8120df-a5c3-4989-8f47-5afc79c56e7c 或者(OR) 姓 名(Name):ttrar 序 列 ...