I offer you 2 variants:
1- For the name of a single client:
(eye! you need a client ID)
Using Linq, filter the client by its identifier and return the name, then make the operation return a string: (this method returns null if the client does not exist)
public String getNombreCliente(int idCliente) {
using (CivarTransporteService.Model.CivarTransporteModelContainer context =
new Model.CivarTransporteModelContainer())
{
return (from c in context.Cliente
where c.id == idCliente
select c.nombre).FirstOrDefault();
}
}
2- For the list of names:
public List<String> getNombreClientes(int idCliente) {
using (CivarTransporteService.Model.CivarTransporteModelContainer context =
new Model.CivarTransporteModelContainer())
{
return (from c in context.Cliente
select c.nombre).ToList();
}
}
Explanation:
Notice that in both cases the trick is in select
, asking for c.nombre
(which I assume is a String
) causes the Linq command to return a IEnumerable<String>
instead of IEnumerable<CivarTransporteService.Model.Cliente>
Then with ToList()
we convert it into a list type List<String>
just what we want to return.
By the way, in the first example using FirstOrDefault
I indicate the IEnumerable<String>
that returns the first record or null if there are no results.
The operations of the WCF service must return the same type as the query functions.
public interface ICatalogsService
{
[OperationContract]
String getNombreCliente(int idCliente);
[OperationContract]
List<String> getNombreClientes();
}
Here is a link with more information about linq: link