none
Help building expression frm a string for passing as argument to method RRS feed

  • Question

  • Say I had a method like this

    public IEnumerable<Store> GetPagedDataRecords(Expression<Func<Store, bool>> expr,
                Expression<Func<Store, object>> sortExpr, int pageNumber, int pageCount)

    How can I build the expression (expr and sortExpr) from a set of string values?


    01000100 01100101 01101111 00100000 01010110 01101001 01101110 01100100 01101001 01100011 01100101

    Friday, June 22, 2012 2:29 PM

Answers

  • I think I might have found something to handle what Im trying to do.

    Dynamic LINQ

    or

    PredicateBuilder

    I think I prefer the type-safe approach of the PredicateBuilder idea


    01000100 01100101 01101111 00100000 01010110 01101001 01101110 01100100 01101001 01100011 01100101


    • Edited by BitShift Tuesday, June 26, 2012 2:23 PM
    • Marked as answer by BitShift Tuesday, June 26, 2012 2:23 PM
    Monday, June 25, 2012 5:16 PM

All replies

  • I'm not 100% clear about what you're trying to do.

    The Expression<> is designed to give you an expression from a lambda function in your source so that you can decompose the query and transform it into a different domain.

    This is the mechanism whereby the LINQ to SQL provider can take something like orders.Where( order => order.quantity > 1 ) and build the SQL: SELECT * FROM orders WHERE quantity > 1.  (or whatever it is... I'm just giving an example...don't complain if my SQL is invalid.)  And it is the mechanism by which you can write your own LINQ query provider.


    So where does the "set of string values" come in?

    Sunday, June 24, 2012 10:31 PM
  • Hi BitShift,

    Welcome to MSDN Forum.

    I agree with @Wyck. Could you please clarify what's the 'a set of string values'?

    Best Regards


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

    Monday, June 25, 2012 2:30 AM
    Moderator
  • The "string values" would be table names to put in the where clause, as well as tabes (multiple) to sort on as well as the sort direction.  In other words, the string values would be set in another layer of the application, and used to build LINQ expressions.  These expressions would then in turn be passed into the layer that contains the method in the original post.


    01000100 01100101 01101111 00100000 01010110 01101001 01101110 01100100 01101001 01100011 01100101


    • Edited by BitShift Monday, June 25, 2012 12:50 PM
    Monday, June 25, 2012 12:49 PM
  • That does seem like a rather roundabout way of doing it.  Why use a C#-language integrated query technique when you are not using the C#-language to build your query?   I mean, I get that you are trying to reuse the GetPagedDataRecords function as is, but this is a little bit complicated, don't you think?  It seems to me that it would be just as easy to build the query directly in SQL, or call a stored procedure.

    Regardless:  the answer seems simple: you can have a map of strings to entities.  But surely if the tables are different, you will lose some of the strong typing.

    Can you give me an example of a query that works with different table names?  If the tables are interchangeable then perhaps your database isn't normalized properly.

    Let me start with something that seems reasonable, given that you haven't defined what "Store" is.

    GetPagedDataRecords( store => store.state == "CA", store => store.name, 0, 0 );

    I assume this much is along the right lines of being valid.  If you want to make use of different tables then you can just grow the complexity of the expressions.

    GetPagedDataRecords( store => SomethingComplex( store, extraData ), store => store.name, 0, 0 );

    The "SomethingComplex" part can do whatever you need to do to get an answer, and if it has to look up other tables, then fine.

    I'm trying to imagine a situation where a table name could be used in place of an entity.  The methods of ITable are not very interesting without knowing the entity type.

    // Even if you had this function, what would you do with the result?
    GetTableFromString( "employees" ).???


    • Edited by Wyck Monday, June 25, 2012 1:27 PM
    Monday, June 25, 2012 1:27 PM
  • I think I might have found something to handle what Im trying to do.

    Dynamic LINQ

    or

    PredicateBuilder

    I think I prefer the type-safe approach of the PredicateBuilder idea


    01000100 01100101 01101111 00100000 01010110 01101001 01101110 01100100 01101001 01100011 01100101


    • Edited by BitShift Tuesday, June 26, 2012 2:23 PM
    • Marked as answer by BitShift Tuesday, June 26, 2012 2:23 PM
    Monday, June 25, 2012 5:16 PM