问题描述:

i want to convert this sql Query to Linq in C#

 select distinct(E.Nombre), E.Puesto,E.Telefono,E.Correo1,E.Correo2

from dbo.CCEscalaMando E

Example output

How can i do?

网友答案:

Let's start off with your SQL:

select distinct(E.Nombre), E.Puesto,E.Telefono,E.Correo1,E.Correo2
    from dbo.CCEscalaMando E

Which technically is valid, but the way it is written makes it look like it is doing something that it isn't. The above will execute the exact same as

SELECT DISTINCT E.Nombre, E.Puesto, E.Telefono, E.Correo1, E.Correo2
FROM dbo.CCEscalaMando E

The parenthesis make it look like you are calling a distinct function, when in reality you are just using SELECT DISTINCT, and the first column you just put in parenthesis for no reason. You could put all the columns in parenthesis, but it has effect on the distinct:

select distinct (E.Nombre), (E.Puesto), (E.Telefono), (E.Correo1), (E.Correo2)
from dbo.CCEscalaMando E

Now that we've removed your confusing bit, it's easy to convert:

dbContext.CCEscalaMando.Select(e=>new {
  e.Nombre,
  e.Puesto,
  e.Telefono,
  e.Correo1,
  e.Correo2})
.Distinct();
相关阅读:
Top