Solved

Calling a function within linq

Posted on 2009-03-31
1
1,070 Views
Last Modified: 2013-11-11
I need to call a function within linq.

I have a linq to entities query:
                Return (From r In oEntities.aspnet_Roles _
                   Where _
                     Not _
                    (From t In oEntities.sssMenuRole _
                    Where _
                      CLng(t.menuId) = menuId2 _
                    Select t.RoleId).Contains(r.RoleId) _
                   Select _
                     r.RoleId, _
                     r.RoleName).ToList()

It receives this error:
LINQ to Entities does not recognize the method 'Boolean Contains[Int32](System.Linq.IQueryable`1[System.Int32], Int32)' method, and this method cannot be translated into a store expression.

In researching on google, I've found a workaround:
http://www.velocityreviews.com/forums/t645784-linq-where-clause.html

It is recommended to use a function such as this to build the contains expression:

    Public Shared Function BuildContainsExpression(Of TElement, TValue)( _
ByVal valueSelector As Expression(Of Func(Of TElement, TValue)), _
ByVal values As IEnumerable(Of TValue) _
) As Expression(Of Func(Of TElement, Boolean))

        ' validate arguments
        If IsNothing(valueSelector) Then Throw New ArgumentNullException("valueSelector")
        If IsNothing(values) Then Throw New ArgumentNullException("values")

        Dim p As ParameterExpression = valueSelector.Parameters.Single()
        If Not values.Any Then
            Return _
            Function(e) False
        End If

        Dim equals = values.Select( _
        Function(v) _
        Expression.Equal(valueSelector.Body, Expression.Constant(v, GetType(TValue))) _
        )

        Dim body = Equals.Aggregate( _
        Function(accumulate, equal) _
        Expression.Or(accumulate, equal) _
        )

        Return Expression.Lambda(Of Func(Of TElement, Boolean))(body, p)
    End Function

My problem is I can't figure out how to rewrite my query to use this method.

Please help?

thanks.
0
Comment
Question by:BobCSD
1 Comment
 
LVL 3

Accepted Solution

by:
dipakdave1983 earned 500 total points
ID: 24036006
calling function is pretty easy with linq/

1) create function into database
2) in VS from serverExplorer, drag function into your LINQ form/page/layout.

now to access it ,

FooBooDatacontext db = new FooBooDatacontext ();//your linq data context

db.yourfunction(param if exist);



0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

In my previous two articles we discussed Binary Serialization (http://www.experts-exchange.com/A_4362.html) and XML Serialization (http://www.experts-exchange.com/A_4425.html). In this article we will try to know more about SOAP (Simple Object Acces…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
You have products, that come in variants and want to set different prices for them? Watch this micro tutorial that describes how to configure prices for Magento super attributes. Assigning simple products to configurable: We assigned simple products…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now