zoukankan      html  css  js  c++  java
  • Lambda 中如果构建一个查询条件,扔该Where返回我们需要的数据。

    有一个需求,比如所 省市县 这三个查询条件

    都可能有可能没有,但是我们的查询条件怎么构建呢

    首先需要看一下 Lambda中Where这个方法需要什么参数

    public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source, Func<TSource, bool> predicate);

    我们需要给他传的参数只有一个   Func<TSource, bool> predicate

    Func 是一个方法

    TSource  是源数据
    bool   条件

    所以创建一个 Func<TSource, bool> 类型的参数就行了

            Func<PartnerInfo, bool> predicate = null;
                if (AreaGroup != 0)
                {
                    predicate = i => i.AreaGroup == AreaGroup;
                }
                if (Province != 0)
                {
                    predicate += i => i.Province == Province;
                }
                if (City != 0)
                {
                    predicate += i => i.City == City;
                }
                if (District != 0)
                {
                    predicate += i => i.District == District;
                }
           return db.PartnerInfo.Where(predicate).ToList();
  • 相关阅读:
    将数据导入PostGIS
    图层管理
    CentIOS PHP 扩展库
    js 笔记 数组(对象)
    JSP 中的 Request 和 Response 对象
    ubuntu 安装 LAMP
    html 学习笔记
    Struts Ajax Json
    Servlet 笔记
    PHP+MYSQL 出现乱码的解决方法
  • 原文地址:https://www.cnblogs.com/ansheng/p/5438593.html
Copyright © 2011-2022 走看看