-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?

9
  • What is DistinctBy?? , LinQ only has Enumerable.Distinct Method. And what is this Select overload learn.microsoft.com/en-us/dotnet/api/…? Does this compile? Commented Aug 14, 2020 at 9:25
  • It does not compile, because the expected type (IEnumerable<user>) is not the same as the actual type List<string>. DistinctBy is from the MoreLINQ extension. Oh and you are correct, the Select is overloaded. Commented Aug 14, 2020 at 9:30
  • Your method is returning IEnumerable<user>. 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 Commented Aug 14, 2020 at 9:30
  • This is the perfect time for a minimal reproducible example With a sample of 4-5 bogus user as input and expected output. Commented Aug 14, 2020 at 9:35
  • I am trying to return a single property or selected properties as a json file, not just a list of values. Commented Aug 14, 2020 at 9:36

1 Answer 1

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

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?
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.
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?
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: learn.microsoft.com/en-us/dotnet/architecture/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.