How to remove two columns from db table in returned LINQ?

Suppose I have the following table structure for employees.

id int
fName varchar
lname varchar
adresss varchar
country varchar

Suppose I created a list employees. in my instructions LINQ, to return all employees to this table, I want the excludecolumns addressand country. Without manually writing the columns that I need in select(), is there any other way to exclude these two columns so that I can make a selection like

db.EMployees.Exclude("Address").Exclude("Country").where(x=>x.lname=="marcus").Select(d=>d);
+4
source share
1 answer

Unfortunately, you cannot do this if you are not projecting a query to get the expected result:

public class EmployeesDTO
{
   public int Id{get;set;}
   public string fName {get;set;}
   //the rest
}

var query= db.EMployees.Where(x=>x.lname=="marcus")
                       .Select(d=>new EmployeesDTO{Id=d.Id, fName=d.Name,...});

Automapper, , ProjectTo :

var query= db.EMployees.Where(x=>x.lname=="marcus").ProjectTo<EmployeesDTO>();
+4

Source: https://habr.com/ru/post/1628155/


All Articles