))] public IHttpActionResult acDetails(st" /> ))] public IHttpActionResult acDetails(st" /> ))] public IHttpActionResult acDetails(st"/>

convert IList object to List

250 Views Asked by At
    [System.Web.Http.HttpGet]
    [System.Web.Http.Route("acDetails")]
    [ResponseType(typeof(List<AutoCompleteCombine>))]
    public IHttpActionResult acDetails(string Text)
    {
                    var db = new ModelName();
                    var DetailsList = (IList)null;
                    try
                    {
                        var DetailsIListXX = (from a in db.Tablename
                                          where a.ColumnName.StartsWith(Text)
                                          select a.ColumnName).Distinct().ToList();
                        DetailsList = DetailsIListXX.Select(x => new { ColumnNameChange= x }).ToArray();
                    }
                    catch (Exception ex)
                    {

                    }
                    return Ok(DetailsList);
     }

//properties
    public class AutoCompleteCombine
    {
        public string ColumnNameChange { get; set; }

    }

Above code works for 'application/json' and 'text/json' response types and gets error for application/xml and text/xml responses in swagger.

Exception Message:The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'

converting the response To List with AutoCompleteCombine class properties would fix the error i think.

Any suggestions to convert ilist to List in this object type case

Thanks

1

There are 1 best solutions below

0
Arsen Khachaturyan On BEST ANSWER

What you are doing with this line:

DetailsList = DetailsIListXX.Select(x => new { ColumnNameChange= x }).ToArray()

is creating Array of anonymous types (see new { ColumnNameChange...}), but instead you need to return List<AutoCompleteCombine>:

DetailsIListXX.Select(x => new AutoCompleteCombine { ColumnNameChange = x }).ToList();