1

1

I want to join two queries into one, how can I do this?

For example:

Query #1:

query = query.Where(q => q.Currency == filter.Currency)

Query #2:

query = query.Where(n => n.FirstName == filter.FirstName)

As a result ,I want to get

query = query.Where(k => k.Currency == filter.Currency && 
                         k.FirstName == filter.FirstName)

Queries are created dynamically, they can include many conditions.


update:

I have a two search types and they can work together. And in the first part there may be several conditions, and in the second one too. The second filter can include two parts with "and" "or"

2
  • 2
    you can chain where if you need it to be
    – Bagus Tesa
    Commented May 20, 2022 at 7:05
  • Do you need to add special operator like contains, greater than, less than ect. To the query as well. Or it would be always equality comparison ?
    – Harish
    Commented May 20, 2022 at 7:06

1 Answer 1

0

I think what you could try is to have two predicates instead of two queries. Sample:

using System;
using System.Collections.Generic;
using System.Linq;

public class Program
{
  public static void Main()
  {
    var pred1 = new Func<int, bool>(f => f % 2 == 0);
    var pred2 = new Func<int, bool>(f => f % 3 == 0);
    foreach (var i in Fibonacci().Take(20).Where(pred1).Where(pred2))
    {
      Console.WriteLine(i);
    }
  }

  private static IEnumerable<int> Fibonacci()
  {
    int current = 1, next = 1;

    while (true) 
    {
      yield return current;
      next = current + (current = next);
    }
  }
}
1

Not the answer you're looking for? Browse other questions tagged or ask your own question.