Select specific Columns in Entity Framework using Linq query in ASP.Net

Last Reply 2 months ago By pandeyism

Posted 2 months ago

 

 var res = tbb.tbl_Enquiry.Where(x => x.Enq_broker == enq).ToList();

I Want to get not all column some specific column from tbl_Enquiry 

Posted 2 months ago

Hi ps222,

Please take reference to the below code 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:GridView ID="gvEmployees" runat="server" AutoGenerateColumns="false">
    <Columns>
        <asp:BoundField DataField="EmployeeName" HeaderText="Name" />
        <asp:BoundField DataField="Country" HeaderText="Country" />
    </Columns>
</asp:GridView>

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    var id = 1;
    NorthwindEntities entity = new NorthwindEntities();
    var result = entity.Employees
                        .Where(X => X.EmployeeID == id)
                        .Select(emp =>
                                new
                                {
                                    EmployeeName = emp.FirstName + " " + emp.LastName,
                                    Country = emp.Country
                                }).ToList();
    this.gvEmployees.DataSource = result;
    this.gvEmployees.DataBind();
}

Screenshot