Solved

Calling a function within linq

Posted on 2009-03-31
1
1,081 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:Starr Duskk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

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…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

726 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