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

nabilabolo
 
on May 21, 2020 12:33 AM
1739 Views

Hi,

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 od.id == item
                        select new DetailModel
                        {
                           order_num = od.order_num,
                           ST = a.ST,
                           pName = b.pName
                        }).ToList();   
                }   

 

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

Hi nabilabolo,

Refer below sample code.

Database

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

Download Northwind Database

HTML

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

Code

C#

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
                                            }).ToList();
            foreach (DetailModel customer in customers)
            {
                customersList.Add(customer);
            }
        }

        gvCustomers.DataSource = customersList;
        gvCustomers.DataBind();
    }
}

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

VB.Net

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
                                                    }).ToList()
            For Each customer As DetailModel In customers
                customersList.Add(customer)
            Next
        Next
        gvCustomers.DataSource = customersList
        gvCustomers.DataBind()
    End If
End Sub

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

Screenshot