Return all records with select new statement in ASP.Net using Linq query

on Apr 05, 2021 01:27 AM

Hi friends,

In below linq query i have selected 1 column which i need, its working fine.

But while debugging in var t it showing all table names from database, how to avoid all table names in this?

public List<tbl> GetImagePeriod()
    var t=(from tp in db.tbl.Where(a => a.TypeID == 17).OrderBy(a => a.TypeListID).ToList()
           select new TypeList
               Name = tp.Name.Trim()
    return t;


Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Apr 05, 2021 02:04 AM

Hi sureshMGR,

You are using Linq query with Lambda expression.

Instead of using both use simple Lambda expression to select the desired column.

Also the function return type should be TypeList instead of tbl.

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


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

Download Northwind Database


<asp:TextBox ID="txtCountry" runat="server" />
<asp:Button Text="Search" runat="server" OnClick="OnSearch" />
<hr />
<asp:GridView ID="gvCustomers" runat="server" AutoGenerateColumns="false"
    AllowPaging="true" PageSize="5" OnPageIndexChanging="OnPageIndexChanging">
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="City" HeaderText="City" />



public List<CustomerList> GetCustomers(string country)
    using (NorthwindEntities entity = new NorthwindEntities())
        var t = entity.Customers
            .Where(a => a.Country == country).OrderBy(a => a.CustomerID)
            .Select(x => new CustomerList
                Id = x.CustomerID,
                Name = x.ContactName.Trim(),
                City = x.City.Trim()
        return t;

protected void OnSearch(object sender, EventArgs e)
    gvCustomers.DataSource = GetCustomers(txtCountry.Text.Trim());

protected void OnPageIndexChanging(object sender, GridViewPageEventArgs e)
    gvCustomers.PageIndex = e.NewPageIndex;
    gvCustomers.DataSource = GetCustomers(txtCountry.Text.Trim());

public class CustomerList
    public string Id { get; set; }
    public string Name { get; set; }
    public string City { get; set; }


Public Function GetCustomers(ByVal country As String) As List(Of CustomerList)
    Using entity As NorthwindEntities = New NorthwindEntities()
        Dim t = entity.Customers _
        .Where(Function(a) a.Country = country) _
        .OrderBy(Function(a) a.CustomerID) _
        .Select(Function(x) New CustomerList With {
            .Id = x.CustomerID,
            .Name = x.ContactName.Trim(),
            .City = x.City.Trim()
        Return t
    End Using
End Function

Protected Sub OnSearch(ByVal sender As Object, ByVal e As EventArgs)
    gvCustomers.DataSource = GetCustomers(txtCountry.Text.Trim())
End Sub

Protected Sub OnPageIndexChanging(ByVal sender As Object, ByVal e As GridViewPageEventArgs)
    gvCustomers.PageIndex = e.NewPageIndex
    gvCustomers.DataSource = GetCustomers(txtCountry.Text.Trim())
End Sub

Public Class CustomerList
    Public Property Id As String
    Public Property Name As String
    Public Property City As String
End Class