Common Method (Function) return Generic List result using Linq query in Entity Framework

nauna
 
on Jun 24, 2019 01:25 AM
Sample_614603.zip
4372 Views

i have this linQ query which return list

i have to create it as method so i can call that method any where in the webiste to assign the result to partial view

like return partialview(result)

List<CheckBoxModel> fc = new List<CheckBoxModel>();
var q = (from s in db.sizes
        select s).Distinct();
var result = q.ToList().Distinct();

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
pandeyism
 
on Jun 24, 2019 03:01 AM

Hi nauna,

Refer below sample.

Namespaces

C#

using NorthwindModel;
using System.Data.Objects;

VB.Net

Imports NorthwindModel
Imports System.Data.Objects

Code

C#

DataAccessCS.cs

public object GetList()
{
    NorthwindEntities entity = new NorthwindEntities();
    var customersList = (from result in entity.Customers
                            select result).Distinct();
    return customersList;
}

CS.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    DataAccess access = new DataAccess();
    List<Customer> list = ((ObjectQuery<Customer>)access.GetList()).ToList();
}

VB.Net

DataAccessVB.vb

Public Function GetList() As Object
    Dim entity As NorthwindEntities = New NorthwindEntities()
    Dim customersList = (From result In entity.Customers Select result).Distinct()
    Return customersList
End Function

VB.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    Dim access As DataAccessVB = New DataAccessVB()
    Dim list As List(Of Customer) = (CType(access.GetList(), ObjectQuery(Of Customer))).ToList()
End Sub

Screenshot