-4

I have a web API method which will return a value in the form of as follows

[ { "TransactionId": "AJ978962017", "Total": 1.17 }, { "TransactionId": "AJ978972017", "Total": 8.9 } ]

I want the output without outer array like as follows

{ "TransactionId": "AJ978962017", "Total": 1.17 }, { "TransactionId": "AJ978972017", "Total": 8.9 }

Kindly suggest how to achieve the desired output.

Code

[Authorize]
// ..api/EmpDetails/Id
public IEnumerable<EmpDetail> Get(string Id)
{
    using (WebAPIEntities WE = new WebAPIEntities())
    {
        var emp = WE.TrxDetails.Where(E => E.EmpId == Id).ToList();
        return emp;
    }
}
15
  • show the code for your method which returns the array of objects. Commented Oct 24, 2017 at 7:14
  • 2
    Possible duplicate of Return JSON object (ASP.NET WebAPI) Commented Oct 24, 2017 at 7:14
  • [Authorize] //..api/EmpDetails/Id public IEnumerable<EmpDetail> Get(string Id) { using (WebAPIEntities WE = new WebAPIEntities()) { var emp = WE.TrxDetails.Where(E => E.EmpId == Id).ToList(); return emp; } } Commented Oct 24, 2017 at 7:20
  • @Jayakumar I'd advise you to format it and put it into the question body. Also, you're returning an List<EmpDetail>(). If you only want one, only return one. Commented Oct 24, 2017 at 7:22
  • 1
    @Jayakumar As soon as you edit your question :) Commented Oct 24, 2017 at 7:29

1 Answer 1

0

The Linq extension method Where(...) returns an IEnumerable<T>. Where is a filter on a list, and as such will return a list (IEnumerable). You're then calling ToList() to convert it into a List<T>. When you serialize to JSON, IEnumerable<T>, List<T>, and T[] are all represented (correctly) as a JSON array: [{...}, {...}, {...}]

[Authorize]
// ..api/EmpDetails/Id
public IEnumerable<EmpDetail> Get(string Id)
{
    using (WebAPIEntities WE = new WebAPIEntities())
    {
        var emp = WE.TrxDetails.Where(E => E.EmpId == Id).ToList();
        return emp;
    }
}

If you only want a single item, there are a few ways you can do this with Linq:

Single(...) - This will return a value so long as that value is unique in the list. It will throw an error if it isn't found.

SingleOrDefault(...) - This will return a value, or null if it isn't found.

First(...) - This will return the first matching value, or throw an error if there aren't any.

FirstOrDefault(...) - This will return the first matching value, or null if there aren't any.

Last(...) - This will return the last matching value, or throw an error if there aren't any.

LastOrDefault(...) - This will return the last matching value, or null if there aren't any.

The exception type returned by the above is: InvalidOperationException

I'm assuming FirstOrDefault(...) will suffice for your scenario:

[Authorize]
// ..api/EmpDetails/Id
public IEnumerable<EmpDetail> Get(string Id)
{
    using (WebAPIEntities WE = new WebAPIEntities())
    {
        return WE.TrxDetails.FirstOrDefault(E => E.EmpId == Id);
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.