Extend IQueryable Where() as OR instead of AND relationship

后端 未结 4 1882
有刺的猬
有刺的猬 2020-12-16 23:15

I am using my own extension methods of IQueryable<> to create chainable queries such as FindAll().FindInZip(12345).NameStartsWith(\"XYZ\").OrderByHowIWantIt() etc. which

4条回答
  •  一向
    一向 (楼主)
    2020-12-17 00:09

        List fruits =
            new List { "apple", "passionfruit", "banana", "mango",
                   "orange", "blueberry", "grape", "strawberry" };
    
        var query = fruits.AsQueryable();
    
        // Get all strings whose length is less than 6.
        query = query.Where(fruit => fruit.Length < 6);
    
        // Hope to get others where length is more than 8.  But you can't, they're gone.
        query = query.Where(fruit => 1 == 1 || fruit.Length > 8);
    
        foreach (string fruit in query)
            Console.WriteLine(fruit);
    

提交回复
热议问题