none
How to reuse Expression<Func<TOHOSPITAL_TBL, int, bool>> in Where operator? RRS feed

  • Question

  • I wanna to reuse the condition about where,so i define the expression as a variable in the class:
    Expression<Func<TOHOSPITAL_TBL, int, bool>> condition = (t, i) => t.RESERVE_ID == i;
    i need to assigned a value to i.
    so,i use it in some method like : _analysisEntity.TOHOSPITAL_TBL.Where(condition);
    but it doesn't work and the variable "i" haven't been used.
    Anyway, does anyone have any hints on it?
    Just On Life!
    Thursday, July 14, 2011 2:30 PM

Answers

All replies

  • Hi Joe,

     

    Welcome to the forum.

    You could have a Custom class to wrap the Expression:

    public class MyExpression

    {

         public static int ID{get;set;}

         public static Expresssion<Func<Address,bool>> Condition

         {

                get{ return (t)=>t.Id==ID;}

         }

    }

     

    Context.Address.Where(MyExpression.Condition);// But we should assign the ID first.

     

    I hope that would be help you.

    Best Regards.


    If it's helpful for you, Please vote or mark. Thank you!

    David Peng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Friday, July 15, 2011 3:43 AM
    Moderator
  • Thank you David,
    Yes,It 's a good solution and i know it can work well.
    but i won't use the class's filed,because a lot of conditions would make the field  too much in the class .
    could i use expression directly to resolve this problem?or with some parameter
    Just On Life!
    Friday, July 15, 2011 3:16 PM
  • Hi Joe,

     

    I'm sorry to reply so late.

    The condition was transfer as a parameter. As I understand once we transfer a parameter to a method the parameter must be valued.

    You could have a mehod to get the filter at the expression.

    Or like:

     .Where("Customer.ID== @0", myId);

     

     


    If it's helpful for you, Please vote or mark. Thank you!

    David Peng [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.





    Tuesday, July 19, 2011 2:44 AM
    Moderator
  • Hi Joe,

    Expression.Call (Dynamic LINQ query)

    http://social.msdn.microsoft.com/Forums/is/linqtosql/thread/cc46a056-40eb-46e2-8ab4-e4b8f74b8c42

     

    I hope this could give you some help.


    • Marked as answer by YuanBin Joe Thursday, July 21, 2011 7:53 AM
    Tuesday, July 19, 2011 3:23 AM
    Moderator
  • Hi,David

    Maybe not a very simple way to disposal the question.

    Dynamic and Expression.Call are both good solutions!

    Thank you for your help!


    Just On Life!
    Thursday, July 21, 2011 7:55 AM