Solved

Calling a function within linq

Posted on 2009-03-31
1
1,073 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
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
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…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

930 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

14 Experts available now in Live!

Get 1:1 Help Now