locked
Trying to add a custom method to return all loggername fields from my data in web api RRS feed

  • Question

  • User1972998160 posted
    		[System.Web.Http.HttpGet]
    		[System.Web.Http.Route("GetByName")]
    		public IEnumerable<Logs> GetByName(string loggername)
            {
    			var log = _repository.GetLogsByName(loggername);
                if (log == null)
                {
                    throw new HttpResponseException(
                        Request.CreateResponse(HttpStatusCode.NotFound));
                }
                else
                {
                    return log;
                }
            }

    I have this simple controller

    and now the dbcontext behind the _repository.GetLogsByName function

    		public List<Logs> GetLogsByName(string lookupname)
    		{
    			List<Logs> mylist = new List<Logs>();
    			mylist = mylist.Where(p => p.LoggerName.Contains(lookupname)).ToList();
    			foreach (var item in mylist)
    			{
    				Logs.Add(item);
    			}
    
    			return mylist;
    		}

    There are no syntax type error but the output is simply [] two square brackets like an empty collection.

    Can anybody help tell me where I've gone wrong?

    Tuesday, July 11, 2017 7:38 PM

All replies

  • User-1220080607 posted

    Hi 

    your code flaw is in GetLogsByName 

    Yor not calling any DbContext. You are doing Simple created List of log object "mylist" ,and you are querying on that object "maylist" which is empty ,and returning that empty object "myLIst" as resut.Since object has no data it showing []. Review your code once agin.

    Your code must look some thing like as follows

    public List<Logs> GetLogsByName(string lookupname)
    		{
    			List<Logs> mylist = new List<Logs>();
    			mylist = yourDbContext.Where(p => p.LoggerName.Contains(lookupname)).ToList();
    			foreach (var item in mylist)
    			{
    				Logs.Add(item);
    			}
    
    			return mylist.count>0?mylist:null;
    		}

    I hope this will help you

    Thanks

    Wednesday, July 12, 2017 2:08 AM
  • User-271186128 posted

    Hi CoderForLife,

    According to your code, I think you need to change two parts.

    1. In repository, you just create a new list, you haven’t connect to database to get data.
    2. In repository, you return a List Log type, but In api method you pass it to IEnumerable Log type.

    I have changed your code, it works fine. You could refer to:

    Code in Repository:

    public class MyRepository
        {
            connStr db = new connStr();
            public IEnumerable<Log> GetLogsByName(string lookupname)
            {
                //connect to database
                IEnumerable<Log> mylist = db.Logs.Where(p => p.LoggerName.Contains(lookupname)).ToList();
                return mylist;// return IEnumerable type
            }
        }
    

    Code in Web Api method:

    		[System.Web.Http.HttpGet]
    		[System.Web.Http.Route("GetByName/{loggername}")]
    		public IEnumerable<Logs> GetByName(string loggername)

    Best regards,
    Dillion

    Wednesday, July 12, 2017 5:06 AM