Add Linq Query to List through Looping using C# and VB.Net in ASP.Net

Last Reply 6 days ago By pandeyism

Posted 6 days ago

hello,

i have this linQ query

var q=(from c in db.country
 select c).ToList();

ListView1.DataSource=q;
ListView1.DataBind()

on run time i want to loop thru the result add additional columns in runt time which i can then bind to listview

 foreach (var item in q) { so here i want to loop thru result and add column "My comments" so with each row return by q it will add one more colimn my comments where i can add value which will bind with listview }

You are viewing reply posted by: pandeyism 6 days ago.
Posted 6 days ago

Hi nauna,

Refer below sample.

HTML

<asp:ListView ID="lvCustomers" runat="server" GroupPlaceholderID="groupPlaceHolder1"
    ItemPlaceholderID="itemPlaceHolder1">
    <LayoutTemplate>
        <table cellpadding="0" cellspacing="0">
            <tr>
                <th>
                    CustomerId
                </th>
                <th>
                    ContactName
                </th>
                <th>
                    Country
                </th>
            </tr>
            <asp:PlaceHolder runat="server" ID="groupPlaceHolder1"></asp:PlaceHolder>
            <tr>
                <td colspan="3">
                    <asp:DataPager ID="DataPager1" runat="server" PagedControlID="lvCustomers" PageSize="10">
                        <Fields>
                            <asp:NextPreviousPagerField ButtonType="Link" ShowFirstPageButton="false" ShowPreviousPageButton="true"
                                ShowNextPageButton="false" />
                            <asp:NumericPagerField ButtonType="Link" />
                            <asp:NextPreviousPagerField ButtonType="Link" ShowNextPageButton="true" ShowLastPageButton="false"
                                ShowPreviousPageButton="false" />
                        </Fields>
                    </asp:DataPager>
                </td>
            </tr>
        </table>
    </LayoutTemplate>
    <GroupTemplate>
        <tr>
            <asp:PlaceHolder runat="server" ID="itemPlaceHolder1"></asp:PlaceHolder>
        </tr>
    </GroupTemplate>
    <ItemTemplate>
        <td>
            <%# Eval("CustomerId") %>
        </td>
        <td>
            <%# Eval("ContactName") %>
        </td>
        <td>
            <%# Eval("Country") %>
        </td>
    </ItemTemplate>
</asp:ListView>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        TestEntities entity = new TestEntities();
        var query = (from c in entity.TestCustomers
                        select c).ToList();
        List<object> result = new List<object>();
        foreach (object item in query)
        {
            result.Add(item);
        }
        lvCustomers.DataSource = result;
        lvCustomers.DataBind();
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handle Me.Load
    If Not Me.IsPostBack Then
        Dim entity As TestEntities = New TestEntities()
        Dim query = (From c In entity.TestCustomers Select c).ToList()
        Dim result As List(Of Object) = New List(Of Object)()

        For Each item As Object In query
            result.Add(item)
        Next

        lvCustomers.DataSource = result
        lvCustomers.DataBind()
    End If
End Sub

Screenshot