Jignesh Gadhia Jignesh Gadhia - 2 months ago 24
C# Question

Convert ObjectResult<string> to List<T>

I want to pass List as Json. How to create list of Member for AM_GAAM property?
Below is my controller's code. I am using stored procedure which returns only one column(AM_GAAM) with multiple rows.


Controller


ObjectResult<string> obj;
obj = objConnection.ADDRESS_MASTER_Select_Distinct_Gaam(objIErrorCode);

if (Convert.ToInt32(objIErrorCode.Value) == 0)
{
list = obj.Select(x => new Member
{
AM_GAAM = x.
}).ToList();
}

return Json(new { Success = true, Message = list }, JsonRequestBehavior.AllowGet);

Answer

If your ADDRESS_MASTER_Select_Distinct_Gaam method returns a list of strings, you can do this

list=obj.Select(s=>new Member { AM_GAM_=x }).ToList();

But if your ADDRESS_MASTER_Select_Distinct_Gaam method is returning a list of some custom object where AM_GAM is a property of that custom class, you can do this

list=obj.Select(s=>new Member { AM_GAM_=x.AM_GAM }).ToList();