user979331 user979331 - 3 months ago 10
ASP.NET (C#) Question

ASP.NET ToList<> Cast bool to string

I have this code here:

GridView gv = new GridView();
gv.DataSource = db.Data.ToList().Where(model => model.closed == false);


and this works great. I was wondering if it possible convert an item in my list from bool to string? something like

gv.DataSource = db.Data.ToList().Where(model => model.closed == false).Cast(convert model.closed to string)

Answer

Project your result to anonymous type and do the conversion there.

GridView gv = new GridView();
gv.DataSource = db.Data.ToList().Where(model => !model.closed)
    .Select(m => new
    {
        Closed = Convert.ToString(m.Closed),
        //... Rest of the fields. 
    });
Comments