Dynamic Linq query based on rules RRS feed

  • Question

  • I'm trying to solve a problem with dynamic rules. The context is, I have to offer courses to students (1) from a region/state (2) anyone who has scored below a grade (3) or new students who have not signed up for a course. Right now through the Admin User Interface, the admin has to enter all the student ids with comma separated when he creates an offer. To get the students list, the DBA has to query the database first with the criteria. This is a tedious process as an admin and a DBA needed to do this.

    To solve the problem, I was thinking of creating few pre-defined search criteria rules and the admin can select it from the User Interface. Instead of retrieving the ids, the admin will select the rule. I can also give the option to the admin to create a dynamic rule like 'Give Offer to Students who are female and at the age between 20 and 22".

    I think this problem can be solved with a list of query specifications if the criteria are pre-defined. Creating a dynamic query from a list of pre-defined verbs and nouns is also possible. But I don't know how to generate a dynamic query specification and save it for the future use. Save it in the database as a sql query?

    Any pattern to solve the above problem is appreciated.

    Sunday, July 1, 2012 1:09 PM


All replies

  • Hi salescrusade,

    Welcome to MSDN Forum.

    Based on this issue, I suggest you to use Entity SQL. In this way, you can create dynamic query based on your rules. For example, you wan to query out students who are female and at the age  between 20 and 22, you can let the 'sex' and 'age' as parameters, to create the query likes below.

    bool Sex = false;
    int ageBegin = 20;
    int ageEnd = 22;
    string query = "SELECT VALUE s FROM YourContext.Students AS s WHERE s.Sex ="+sex+" And s.Age between "+ageBegin+" and "+ageEnd;

    Best Regards

    Allen Li [MSFT]
    MSDN Community Support | Feedback to us

    Monday, July 2, 2012 7:39 AM
  • I had a chance to read an article written by Scott Hanselman that deals with dynamic query. I may take that direction because though I can use EntitySql I dont want to mix both LINQ and EntitySQL.
    Saturday, July 7, 2012 9:50 PM