-1

I want to create and return an object in this return call:

var Zones = _ZoneService.GetZones();
var userZones = _ZoneService.GetUserZones(user);
return Ok(//Here I need to return both Zones and userZones);

How can I create new on the go object to return these two. Thanks...

8
  • 2
    Does return OK(new { Zones, userZones}); work? Commented Sep 25, 2018 at 10:03
  • 2
    What is Ok? A method? Your question is hard to answer unless you provide the signature of the containing method and what Ok is. Commented Sep 25, 2018 at 10:05
  • 1
    @HimBromBeere Ok is function from asp.net core controller base. Commented Sep 25, 2018 at 10:08
  • 1
    Thanks all. This return OK(new { Zones, userZones}); worked for me. @HimBromBeere Ok is the response in case of WebApi in .net core Commented Sep 25, 2018 at 10:09
  • Possible duplicate of Return Multiple Objects Using ASP.NET MVC'S JsonResult Class Commented Sep 25, 2018 at 10:26

3 Answers 3

6

What about Anonymous Types? Something like this:

return Ok(new { Zones, userZones });
Sign up to request clarification or add additional context in comments.

Comments

0

I think that the best way to go is to creat a list, add the object to your list, and return the list. This way is also easyer if you need to work with these object.

To create a list, first we need to code a propper class:

public class Item {
    public int Id { get; set; }
    public Object MyObj { get; set; }
}

Now we can create and populate the list:

List<Item> items = new List<Item>()
{
    new Item{ Id=1, MyObj= Zones},
    new Item{ Id=2, MyObj= userZones}
}

Now you can return your list using: return items

Comments

-2

You could also use anout parameter, like:

public Zones GetZones(..., out Zones userZones)
{
    uzerZones = _ZoneService.GetUserZones(user);
    return _ZoneService.GetZones();
}

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.