-1
public class UserController : ApiController
    {
        UserSampleEntities entities = new UserSampleEntities();
      
        // GET api/<controller>
        [Route("api/User")]
        public IEnumerable<user> Get()
        {

            {
                return entities.users;

            }
        }
   }

This returns the json with all the entries in the database with all its properties. How do I filter such that I can obtain a json for only specific properties?

pepega
  • 25
  • 8
  • What is `DistinctBy`?? , LinQ only has [`Enumerable.Distinct Method`](https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.distinct). And what is this Select overload https://learn.microsoft.com/en-us/dotnet/api/system.linq.enumerable.select? Does this compile? – Drag and Drop Aug 14 '20 at 09:25
  • It does not compile, because the expected type (IEnumerable) is not the same as the actual type List. DistinctBy is from the MoreLINQ extension. Oh and you are correct, the Select is overloaded. – pepega Aug 14 '20 at 09:30
  • Your method is returning `IEnumerable`. I expect element from `entities.users` to be type of `user`. So what are you trying to return as object new user with only some poperties initialise so perhaps `Select(x=> new user{prop1= x.Prop1})`? or are you trying to select like this : [Select Multiple Fields from List in Linq](https://stackoverflow.com/questions/1202981/select-multiple-fields-from-list-in-linq) – Drag and Drop Aug 14 '20 at 09:30
  • This is the perfect time for a [Mre] With a sample of 4-5 bogus user as input and expected output. – Drag and Drop Aug 14 '20 at 09:35
  • I am trying to return a single property or selected properties as a json file, not just a list of values. – pepega Aug 14 '20 at 09:36
  • Json cool. Still not clarifying the question. For you it looks clear for me each line of this example condradict an other. Define a `user` class with few property one for the distinct key, one that you need to send back one that you don't need. Create few user fill those property. Show the expected result. You don't wanna share the whole class nor the definition, sure just give use an Json example of input and expected output. That's will be like 10 lines for the inputs, we will deduce the calsse base on the json name with a simple copy past. I'm not asking for real data. – Drag and Drop Aug 14 '20 at 09:45
  • Ok so if I change the type to List it will return ["Male","Female"], but I want it to return [{"gender": "Male"}, {"gender": "Female"}] – pepega Aug 14 '20 at 09:47
  • The problem is when I use Select, it becomes a list of strings. So I either have to find another way to select specific properties, or I find a way for the service to accept list of strings information – pepega Aug 14 '20 at 09:59
  • https://dotnetfiddle.net/GdChnK , there should be a dupe about linq select on SO. – Drag and Drop Aug 14 '20 at 10:03

1 Answers1

0

Create a new class that represents a user using only the properties you want to expose from "api/User":

public class UserDto
{
    public int Foo { get; set; }

    public string Bar { get; set; }

    // add the properties you need here
}

Rewrite your API action to this:

[Route("api/User")]
public IEnumerable<UserDto> Get()
{
    return entities.users
        .Select(u => new UserDto
        {
            Foo = u.Foo,
            Bar = u.Bar,
            // map the properties you need here
        })
        .ToArray();
}
Rudey
  • 4,717
  • 4
  • 42
  • 84
  • Can I ask what is the best practice if I need to make a cascaded drop down list? Do I create separate API actions and add the conditions in the service? Or do I add the conditions in this controller file? – pepega Aug 14 '20 at 10:14
  • Generally speaking, you shouldn't put logic in controllers or even directly use the database context in your controllers. You should move everything to services, and keep the controllers as basic as possible. – Rudey Aug 14 '20 at 10:21
  • Ok, so I am trying to create a form in a web application using Angular for front end and asp.net mvc for backend. I managed to import a database into asp.net mvc using ADO.NET and from there I created a web api 2 controller to retrieve the entities and return them as a json output. I have also created a service in Angular to subscribe to this web api to obtain the information from the database. So by service do you mean the Angular service, or a separate service in asp.net? – pepega Aug 14 '20 at 10:24
  • By service I meant a class/interface in your backend that is responsible for a single purpose, for example mapping database entities into something that clients can work with (User -> UserDto mapping is an example of this). If you're interested, you should read this article about web app architecture: https://learn.microsoft.com/en-us/dotnet/architecture/modern-web-apps-azure/common-web-application-architectures – Rudey Aug 14 '20 at 11:47