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
  • If your filters are known at development time, then, you just needt to write the proper code.

    If your filters are known ONLY at runtime, then, there is no chance, because the assembly created is IMMUTABLE, therefore you cannot add names or code into it, besides many other things.

    That said, parsing the *filter* object is not an option, because at development time you need to know how to parse that object. This means you need to write an SPECIFICATION related to tbis object telling its syntax, semantics, rules, etc... If you do this, then you'll be able to write at development time a parser, to parse such objects.

    One possible solution for you, is define a group of possiblend fixed filters, that the user can choose at runtime, and write the code, at development time, for each one of those filters.

    Friday, February 17, 2017 6:40 PM
  • 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
  • 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.

    It seems that you understood my post, thus, I feel free to mark my reply as correct answer ( you should do the same).

    PS: as I wrote in my post, if you define a set of possible "filters" to be used at runtime, then you'll know at development time, what filter to expect.

    Wednesday, February 22, 2017 8:58 PM
  • 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

    It is not possible o anticipate what will happen at runtime.

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

    Should this be correct: (x) and (y)

    and this: ((x)) and (y)

    or even this: x and (y)

    and etc...

    Anyone with little experience in programming, knows how hard it is to detect ALL possible wrong entries from a user

    Try to be professional and do not unmark my post as correct...



    • Edited by ritehere34 Thursday, February 23, 2017 12:51 AM colorizing
    Thursday, February 23, 2017 12:49 AM
  • 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
  • Building a query at runtime is doable, BUT IT HAS A SPECIFICATION.

    For instance, is this a query: x,y++y))!

    Or should the query obey some syntax, etc...?


    • Edited by ritehere34 Thursday, February 23, 2017 1:06 AM
    Thursday, February 23, 2017 1:05 AM
  • Also, note that the OP post a conclusion that matches my post!


    Thursday, February 23, 2017 1:08 AM
  • 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