Tuesday, September 24, 2019

EF Core select specific columns in a table

In the previous posts for CRUD operations we saw on how to select all the column values from a context table, sometimes we will not need all the columns from a table instead we will need only specific columns from a table. In these cases we can use a Select () clause to filter specific columns from a table. In this post we shall see on how to select only the Id, Name and Age properties from the employee table.

The employee table has the following columns. 




By default if we use _context.Employee.ToList() EF Core will retrieve data for all the columns in the table, instead we will use the Select () clause as follows to get only the Id, Name and Age column values.

            var _context = new UserRegistrationContext();
            var employeeList = _context.Employee
                                    .Select(e => new EmployeeDetail
                                    {
                                        EmployeeId = e.EmployeeId,
                                        Name = e.Name,
                                        Age = e.Age
                                    }).ToList();

Search Flipkart Products:
Flipkart.com

No comments: