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);
}
}
List<EmpDetail>(). If you only want one, only return one.