Bind (Save) database values in Generic List using Linq in ASP.Net

on May 21, 2020 12:33 AM


How to save data from database into list in linq.

I have foreach loop which it loop the data from database. I want to save the result in array and I want to use the array outside the loop. How can i do that? 

               foreach (var item in chkST)
                    var selectedOrder = (from od in db.Cust
                        join a in db.Order
                        on od.order_num equals a.order_no
                        join b in db.OrderList
                        on od.ph_num equals b.pNum
                        where == item
                        select new DetailModel
                           order_num = od.order_num,
                           ST = a.ST,
                           pName = b.pName


Download FREE API for Word, Excel and PDF in ASP.Net: Download
on May 21, 2020 01:44 AM
on May 22, 2020 03:59 AM

Hi nabilabolo,

Refer below sample code.


For this example I have used Customers table of Microsoft Northwind Database. You can download the database using the link below.

Download Northwind Database


<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false">
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="City" HeaderText="City" />
        <asp:BoundField DataField="Country" HeaderText="Country" />



protected void Page_Load(object sender, EventArgs e)
    if (!IsPostBack)
        string[] countries = new[] { "Sweden", "Mexico", "UK" };
        List<DetailModel> customersList = new List<DetailModel>();
        foreach (string country in countries)
            NorthwindEntities db = new NorthwindEntities();
            List<DetailModel> customers = (from c in db.Customers
                                            where c.Country == country
                                            select new DetailModel
                                                Name = c.ContactName,
                                                City = c.City,
                                                Country = c.Country
            foreach (DetailModel customer in customers)

        gvCustomers.DataSource = customersList;

public class DetailModel
    public string Name { get; set; }
    public string City { get; set; }
    public string Country { get; set; }


Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not IsPostBack Then
        Dim countries As String() = {"Sweden", "Mexico", "UK"}
        Dim customersList As List(Of DetailModel) = New List(Of DetailModel)()
        For Each country As String In countries
            Dim db As NorthwindEntities = New NorthwindEntities()
            Dim customers As List(Of DetailModel) = (From c In db.Customers
                                                        Where c.Country = country
                                                        Select New DetailModel With {
                                                        .Name = c.ContactName,
                                                        .City = c.City,
                                                        .Country = c.Country
            For Each customer As DetailModel In customers
        gvCustomers.DataSource = customersList
    End If
End Sub

Public Class DetailModel
    Public Property Name As String
    Public Property City As String
    Public Property Country As String
End Class