locked
Lambda Expression in Customers_Filter method RRS feed

  • Question

  • I have a rather complicated business scenario where I have to filter the Customers to the users according to intrincated business rules.

    I have defined a Function that gets one Customer Entity and returns a Boolean value as a result and then tried to use it in the Customers_Filter method as a lambda expression.

    But the Function is totally ignored.

    Questions:

    1. Is possible to call user defined functions inside the Customers_Filter method?

    2. How must I implement this kind of function?

    Jean Pierre Chauny

    Friday, May 31, 2013 12:49 AM

All replies

  • This should be possible.  Can you provide more details?  Possibly show what you attempted in the filter method?


    Brian

    Friday, May 31, 2013 1:32 AM
  • Thank you for your reply. I have defined some extension methods for a LS entity called Customers. One function gets a Customer and returns a Boolean value. Then I call this function in the lambda expression of the Customers_Filter method in order to implement Row Level Security. But the system ignores the function and filters out all Customers. I need to know if it is possible to use function calls in order to build the lambda expressions in the Customers_Filter method. The parameter passed by reference called filter is used somewhere else to buil a query and filter out the Customers. I see two options: 1. It is not possible to use function calls for building the lambda expressions. 2. The code that calls Customers_Filter has my extension methods not in scope. Help is really appreciated. Jean Pierre Chauny
    Friday, May 31, 2013 9:46 PM
  • Jean Pierre,

    Showing the code would make it easier to see what's going wrong.

    I'm not sure that an extension method is here the right way to proceed, it depends on how you use it.

    If you have following lambda: c=>c.LastName = MyMethod(c). Is should work.

    But I'm not sure if c=>c.MyMethodAsExtensionMethod() = somethingelse would work.


    paul van bladel

    Saturday, June 1, 2013 7:34 AM
  • I have written a Word document trying to document in more detail the problem.

    https://www.dropbox.com/s/6apfm4dml4l2qvs/Documenting%20Lambda%20Expression%20Problem.docx?v=0mcns

    Jean Pierre Chauny

    Saturday, June 1, 2013 7:01 PM
  • Paul, I am not able to invoke any own methods inside the _Filter method (it doesn't matters if it is a static, an instance or an extension method. The _Filter method passes a parameter called filter ByRef. This parameter is of type Expression(Of Func(Of Customer,Boolean)). The question is: how do you construct an Expression(Of Func(Of Customer,Boolean)) out of a method call tha gets a Customer and returns a Boolean? It seems that the Linq to Sql in LS needs an expression tree rather than a function delegate.
    Monday, June 3, 2013 2:02 PM
  • Sure, Linq needs to be able to transform the method to sql eventually.

    But, is it not possible to first call your method and store the result in a variable and then as step 2 invoke the lambda ?


    paul van bladel

    Monday, June 3, 2013 2:24 PM
  • Public Function IsCurrentUserOwner(ByRef c As Customer) As Boolean
    'Your code that is recursive and complicated to express as a System.Linq.Expressions.Expression comes here
    End Function
    
    
    Private Sub Customers_Filter(ByRef filter As System.Linq.Expressions.Expression(Of System.Func(Of Customer, Boolean)))
    
    filter = Function(c) IsCurrentUserOwner(c) = True
    
    End Sub

    This is the code snippet that causes the error.

    I dont understand how to call the method first and then invoke the lambda?

    Do you mean to store the result in a Customers field and then invoke the lambda?

    Thank you for your patience.



    Monday, June 3, 2013 3:42 PM
  • You should try to reverse the logic and apply the filter on the "external" data rather than on the lambda expression variable:

     partial void Customers_Filter(ref Expression<Func<Customer, bool>> filter)
            {
                IEnumerable<string> Managers = MyComplicatedMethod(); ;
    
                filter = e => Managers.Contains(e.Manager);
            }
    
            private IEnumerable<string> MyComplicatedMethod()
            {
                return new List<string> { "TestUser" };
            }


    paul van bladel

    Monday, June 3, 2013 4:36 PM
  • Very interesting!

    I will give it a try and report the results.

    Thank you very much!

    Monday, June 3, 2013 4:40 PM
  • @Amy - please do not propose a reply one day, then mark it as the answer the next. CSS has been repeatedly asked to not mark answers just for the sake of closing a thread.

    It's up to the OP to decide if the proposed reply answers their question or not, not us moderators. We're here to help the forum, not to interfere by closing threads for the sake of stats.


    Yann Duran
         - Co-Author of Pro Visual Studio LightSwitch 2011
         - Author of the  LightSwitch Central Blog

    FREE Download: Luminous Tools for LightSwitch
    (a Visual Studio productivity extension for LightSwitch)
     
    Click Mark as Answer, if someone's reply answers your question
    Click  Vote as Helpful, if someone's reply is helpful
     
    By doing this you'll help everyone find answers faster.

    Friday, June 7, 2013 4:23 AM
    Moderator