none
writing linq query with varying search criteria

    Question

  • Hello,

    I have an interesting problem to do with filtering linq results and I'm wondering if there's a way to do this.

    First, I am given a string of search filters of the form:

    "(x and y and z) or (a and b and c) or..."

    Then I have an IEnumerable like so:

    var result = context.GetResults();

    The challenge is to use the search filter string on result using linq. With the above search filter string, the linq query would be:

    return result.Where(d => (d.description.Contains(x) && d.description.Contains(y) && d.description.Contains(z))
    || (d.description.Contains(a) && d.description.Contains(b) && d.description.Contains(c)));

    However, I'm not always going to get that exact search filter string. I may get this:

    "(x and y) or (z)"

    or this:

    "(x) or (y) or (z)"

    or this:

    "(x and y and z and a)"

    Can anyone think of a way to write a generic linq query that can handle varying ands/ors between the search keywords?

    Thanks.
    Thursday, February 16, 2017 10:23 PM

All replies

  • Hi gib898,

    You may have a wrong logic.  Linq cannot help you make a condition to judge. You should make this  filter  condition use a if,,, else statement.

    Best regards,

    Kristin


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, February 17, 2017 9:11 AM
    Moderator
  • If x,y and z are names of fields/members on the object you're using LINQ for then convert it to a lambda expression. There are several libraries available that do this. Alternatively you could spin up the CSharpCodeProvider and basically do the same thing. None of this will be fast though.

    Yet another option is to build a simple parser that understands the syntax you'll allow and generates the appropriate LINQ statements.

    Michael Taylor
    http://www.michaeltaylorp3.net

    Friday, February 17, 2017 3:06 PM
    Moderator
  • Thanks everyone,

    Since the query is only known at run time, it doesn't look like this will be possible.

    Thanks for your feedback anyway.

    Wednesday, February 22, 2017 8:40 PM
  • Please do not mark your own posts as proposed answers. In these forums the OP or the administrators will mark the appropriate posts. By making a post you are proposing an answer. The OP is the only one who can determine which post or posts is the best. Thank you.

    "Thus, your proposal are empty of value, while a FULL SPECIFICATION is written, AND a AVAILABLE to the end user."

    I don't understand what you are trying to get at here but supporting the concept of building a query at runtime is doable and is, in fact, done quite a bit. The whole purpose of the Binder in ASP.NET is to solve this very problem. Please be respectful of other opinions and refrain from being rude or your posts will be reported.

    Thursday, February 23, 2017 1:01 AM
    Moderator
  • Hi gib898,

    If your issue is solved please Mark as answer or Vote as helpful post to the appropriate answer so that it will help other members to find solution if they faces similar issue.

    Your understanding and cooperation will be grateful.

    Thanks,
    Sabah Shariq

    [If a post helps to resolve your issue, please click the "Mark as Answer" of that post or click Answered "Vote as helpful" button of that post. By marking a post as Answered or Helpful, you help others find the answer faster. ]

    Monday, February 27, 2017 9:20 AM
    Moderator