Common Method or Function return Linq query result in Entity Framework

Last Reply 3 months ago By dharmendr

Posted 3 months ago

hello,

i have this linQ query which return list i want to create a method of it so i can reuse that method again and agian

pls advice

var q = (from c in entities.category
         select c );

 

Posted 3 months ago

Hi nauna,

You can create a method that will return Anonymous types as an object.

Check this example. Now please take its reference and correct your code.

Database

For this example I have used of Northwind database that you can download using the link given below.

Download Northwind Database

C#

protected void Page_Load(object sender, EventArgs e)
{
    var employee = GetEmployees();
}

private object GetEmployees()
{
    NorthwindEntities entities = new NorthwindEntities();
    var q = from emp in entities.Employees
               select emp;

    return q;
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim employee = GetEmployees()
End Sub

Private Function GetEmployees() As Object
    Dim entities As NorthwindEntities = New NorthwindEntities()
    Dim q = From emp In entities.Employees Select emp
    Return q
End Function