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

Last Reply 6 days ago By dharmendr

Posted 6 days ago

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()
           }).ToList();
    return t;
}

 

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

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.

Database

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

Download Northwind Database

HTML

<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">
    <Columns>
        <asp:BoundField DataField="Id" HeaderText="Id" />
        <asp:BoundField DataField="Name" HeaderText="Name" />
        <asp:BoundField DataField="City" HeaderText="City" />
    </Columns>
</asp:GridView>

Code

C#

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()
            }).ToList();
        return t;
    }
}

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

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

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

VB.Net

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()
        }).ToList()
        Return t
    End Using
End Function

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

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

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

Screenshot