0

Hi I am doing web api project. I have one get http verb and i want to return multiple rows of data. I am using angular in front end. I am trying as below.

public IEnumerable<NCT_Process_Settings> Get()
{
    NCT_Process_Settings obj = (from c in entityObject.NCT_Process_Settings select c).AsEnumerable();
    return obj;
}

As i know my query returns collection and my object obj is not capable of handling multiple rows of data. So is there any way i can handle this? Any help would be appreciated.Thank you.

3
  • 1
    IEnumerable<NCT_Process_Settings> obj =(from .... (your query returns a collection, not a single object Commented Feb 3, 2017 at 11:26
  • Thank you. Please post answer. Worked... Commented Feb 3, 2017 at 11:28
  • You could also use var obj = (from ... which would infer the type Commented Feb 3, 2017 at 11:31

3 Answers 3

2

Try this one

        [Route("api/test")]
        public HttpResponseMessage Get()
        {
            HttpResponseMessage response = null;
            List<table_name> obj = new List<table_name>();
            try
            {
                obj = db.table_name.AsNoTracking().ToList();

                response = Request.CreateResponse(HttpStatusCode.OK, obj);
            }
            catch (Exception ex)
            {
                response = Request.CreateResponse(HttpStatusCode.NotFound);
            }
            return response;
        }

then access this api with http://192.168.0.1/api/test

table_name can be a model

Sign up to request clarification or add additional context in comments.

Comments

1

You declare a variable which is a single object, but your trying to assign a collection of objects to it (the result of your query). Change the code to

IEnumerable<NCT_Process_Settings> obj = (from c in entityObject.NCT_Process_Settings select c).AsEnumerable();
return obj;

or

var obj = (from c in entityObject.NCT_Process_Settings select c).AsEnumerable();
return obj;

3 Comments

Thank you. If i get rows to var then IEnumerable<NCT_Process_Settings> will it work? Sorry if i am wrong.
using var will work fine (internally, the compiler infers the type based on what your query returns so its the same as IEnumerable<NCT_Process_Settings> ). And as a side note, you may not need the .AsEnumerable() which makes it an in-memory set. You could just return IQueryable<NCT_Process_Settings> which might be more efficient depending on what you then do with that collection
Thank you. I understood and thank you for making me to understand the concept.
-1

You can try this, its working fine

public static List<Student> items = new List<Student>();


        [HttpGet]
        public IEnumerable<Student> GetAllStudents()
        {
            if (items.Count() == 0)
            {
                items.Add(new Student { ID = 1, Name = "Ashish", City = "New Delhi", Course = "B.Tech" });
                items.Add(new Student { ID = 2, Name = "Nimit", City = "Noida", Course = "MCA" });
                items.Add(new Student { ID = 3, Name = "Prawah", City = "Dehradun", Course = "B.Tech" });
                items.Add(new Student { ID = 4, Name = "Sumit", City = "Nainital", Course = "MCA" });
            }

            return items;
        }

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.