Shawn L.
Shawn L.

Reputation: 179

Convert List<Object> to ByteArray

Currently, I'm sending my objects from server side to client side using JsonResults.

The current code is as follows:

[HttpGet]
    public JsonResult Get()
    {
        //Create a List object, categoryList which can store anonymous objects later.
        List<object> categoryList = new List<object>();

        var categoriesQueryResult = Database.Categories
                 .Where(eachCategory => eachCategory.DeletedAt == null)
                 .Include(eachCategory => eachCategory.CreatedBy)
                 .Include(eachCategory => eachCategory.UpdatedBy);

        //Loop through each Category entity in  the categoriesQueryResult's and add it into categoryList
        foreach (var oneCategory in categoriesQueryResult)
        {
            categoryList.Add(new
            {
                categoryId = oneCategory.CategoryId,
                categoryName = oneCategory.CategoryName,
                visibility = oneCategory.Visibility,
                displayStart =  oneCategory.DisplayStart,
                displayEnd = oneCategory.DisplayEnd,
                createdAt = oneCategory.CreatedAt,
                updatedAt = oneCategory.UpdatedAt,
                createdBy = oneCategory.CreatedBy.FullName,
                updatedBy = oneCategory.UpdatedBy.FullName,
                isSpecial = oneCategory.IsSpecial

            });
        }//end of foreach

        return new JsonResult(categoryList);
    }// end of get

So what I would like to do now is to convert the List to a ByteArray and then return the converted object to the client side as a ByteArray.

I've tried:

[HttpGet]
    public byte[] Get()
    {
        //Create a List object, categoryList which can store anonymous objects later.
        List<object> categoryList = new List<object>();

        var categoriesQueryResult = Database.Categories
                 .Where(eachCategory => eachCategory.DeletedAt == null)
                 .Include(eachCategory => eachCategory.CreatedBy)
                 .Include(eachCategory => eachCategory.UpdatedBy);

        //Loop through each Category entity in  the categoriesQueryResult's and add it into categoryList
        foreach (var oneCategory in categoriesQueryResult)
        {
            categoryList.Add(new
            {
                categoryId = oneCategory.CategoryId,
                categoryName = oneCategory.CategoryName,
                visibility = oneCategory.Visibility,
                displayStart =  oneCategory.DisplayStart,
                displayEnd = oneCategory.DisplayEnd,
                createdAt = oneCategory.CreatedAt,
                updatedAt = oneCategory.UpdatedAt,
                createdBy = oneCategory.CreatedBy.FullName,
                updatedBy = oneCategory.UpdatedBy.FullName,
                isSpecial = oneCategory.IsSpecial

            });
        }//end of foreach
        byte[] convertedObject = categoryList.OfType<byte>().ToArray();

        return convertedObject;
    }// end of get

But it does not seem to work.

Upvotes: 4

Views: 13462

Answers (1)

Ashish Shukla
Ashish Shukla

Reputation: 1294

You can use BinaryFormatter to get the byte array.

byte[] bytes = null;
BinaryFormatter bf = new BinaryFormatter();
using (MemoryStream ms = new MemoryStream())
{
    bf.Serialize(ms, categoryList);
    bytes = ms.ToArray();
}

Upvotes: 8

Related Questions