LINQ 学习路程 -- 查询操作 where

1.where

  

Filtering Operators Description
Where Returns values from the collection based on a predicate function
OfType Returns values from the collection based on a specified type. However, it will depend on their ability to cast to a specified type.
public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
                                                  Func<TSource, bool> predicate);

public static IEnumerable<TSource> Where<TSource>(this IEnumerable<TSource> source,
                                                  Func<TSource, int, bool> predicate);
IList<Student> studentList = new List<Student>() {
        new Student() { StudentID = 1, StudentName = "John", Age = 13} ,
        new Student() { StudentID = 2, StudentName = "Moin",  Age = 21 } ,
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 18 } ,
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20} ,
        new Student() { StudentID = 5, StudentName = "Ron" , Age = 15 }
    };

var filteredResult = from s in studentList
                    where s.Age > 12 && s.Age < 20
                    select s.StudentName;

方式2

Func<Student,bool> isTeenAger = delegate(Student s) {
                                    return s.Age > 12 && s.Age < 20;
                                };

var filteredResult = from s in studentList
                     where isTeenAger(s)
                     select s;

方式3

public static void Main()
{
    var filteredResult = from s in studentList
                         where isTeenAger(s)
                         select s;
}

public static bool IsTeenAger(Student stud)
{
    return stud.Age > 12 && stud.Age < 20;
}

where的第二个扩展方法包含集合的index索引

IList<Student> studentList = new List<Student>() {
        new Student() { StudentID = 1, StudentName = "John", Age = 18 } ,
        new Student() { StudentID = 2, StudentName = "Steve",  Age = 15 } ,
        new Student() { StudentID = 3, StudentName = "Bill",  Age = 25 } ,
        new Student() { StudentID = 4, StudentName = "Ram" , Age = 20 } ,
        new Student() { StudentID = 5, StudentName = "Ron" , Age = 19 }
    };

var filteredResult = studentList.Where((s, i) => {
            if(i % 2 ==  0) // if it is even element
                return true;

        return false;
    });

foreach (var std in filteredResult)
        Console.WriteLine(std.StudentName);

多个where从句

var filteredResult = from s in studentList
                    where s.Age > 12
                    where s.Age < 20
                    select s;
var filteredResult = studentList.Where(s => s.Age > 12).Where(s => s.Age < 20);

需要记住的几点:

1.Where根据特定条件来筛选集合元素

2.where扩展方法有2个重载,使用第二个重载方法可以知道当前元素在集合中的索引位置

3.方法语法需要整个lambda表达式,而查询语法仅仅需要表达式主体

4.在单一的LINQ查询中可以使用多个where从句

时间: 2024-08-05 16:23:50

LINQ 学习路程 -- 查询操作 where的相关文章

LINQ 学习路程 -- 查询操作 OrderBy &amp; OrderByDescending

Sorting Operator Description OrderBy 通过给定的字段进行升序 降序 排序 OrderByDescending 通过给定字段进行降序排序,仅在方法查询中使用 ThenBy 第二级升序排序,仅在方法查询中使用 ThenByDescending 第二级降序排序,仅在方法查询中使用 Reverse 反转集合,仅在方法查询中使用 IList<Student> studentList = new List<Student>() { new Student()

LINQ 学习路程 -- 查询操作 Expression Tree

表达式树就像是树形的数据结构,表达式树中的每一个节点都是表达式, 表达式树可以表示一个数学公式如:x<y.x.<.y都是一个表达式,并构成树形的数据结构 表达式树使lambda表达式的结构变得透明清楚, Expression<Func<Student, bool>> isTeenAgerExpr = s => s.age > 12 && s.age < 20; 编译器将上面的表达式翻译成下面的表达式树 Expression.Lambda

LINQ 学习路程 -- 查询操作 Deferred Execution of LINQ Query 延迟执行

延迟执行是指一个表达式的值延迟获取,知道它的值真正用到. 当你用foreach循环时,表达式才真正的执行. 延迟执行有个最重要的好处:它总是给你最新的数据 实现延迟运行 你可以使用yield关键字实现延迟加载 public static class EnumerableExtensionMethods { public static IEnumerable<Student> GetTeenAgerStudents(this IEnumerable<Student> source)

LINQ 学习路程 -- 查询操作 Join

Join操作是将两个集合联合 Joining Operators Usage Join 将两个序列连接并返回结果集 GroupJoin 根据key将两个序列连接返回,像是SQL中的Left Join Join操作两个集合,inner collection 和 outer collection 它返回一个集合(包含两个集合根据特定条件结合的所有元素),和SQL中的inner join一样 public static IEnumerable<TResult> Join<TOuter, TIn

LINQ 学习路程 -- 查询操作 OfType

OfType操作根据集合中的元素是否是给定的类型进行筛选 IList mixedList = new ArrayList(); mixedList.Add(0); mixedList.Add("One"); mixedList.Add("Two"); mixedList.Add(3); mixedList.Add(new Student() { StudentID = 1, StudentName = "Bill" }); var stringR

LINQ 学习路程 -- 查询操作 let into关键字

IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", Age = 18 } , new Student() { StudentID = 2, StudentName = "Steve", Age = 21 } , new Student() { StudentID = 3, StudentN

LINQ 学习路程 -- 查询操作 GroupBy ToLookUp

Grouping Operators Description GroupBy GroupBy操作返回根据一些键值进行分组,每组代表IGrouping<TKey,TElement>对象 ToLookup ToLookup is the same as GroupBy; the only difference is the execution of GroupBy is deferred whereas ToLookup execution is immediate. IList<Stude

LINQ 学习路程 -- 查询操作 ThenBy &amp; ThenByDescending

IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", Age = 18 } , new Student() { StudentID = 2, StudentName = "Steve", Age = 15 } , new Student() { StudentID = 3, StudentN

LINQ 学习路程 -- 查询操作 Quantifier Operators All Any Contain

Operator Description All 判断所有的元素是否满足条件 Any 判断存在一个元素满足条件 Contain 判断是否包含元素 IList<Student> studentList = new List<Student>() { new Student() { StudentID = 1, StudentName = "John", Age = 18 } , new Student() { StudentID = 2, StudentName