Solved

Calling a function within linq

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

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…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…

617 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