2017-04-13 124 views
0

目前,我使用JsonResults將我的對象從服務器端發送到客戶端。Convert List <Object> to ByteArray

當前的代碼如下:

[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 

所以我想現在要做的就是將列表轉換爲ByteArray,然後轉換後的對象爲ByteArray返回給客戶端。

我已經試過:

[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 

但它似乎並沒有工作。任何答覆或幫助將不勝感激。提前致謝。

+0

爲什麼要將其轉換爲字節數組?客戶如何設想理解它? –

回答

1

您可以使用BinaryFormatter來獲取字節數組。

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

請注意匿名類型不能被'BinaryFormatter'序列化,所以你需要創建''Serializable''類型並將'categoryList'的類型改爲'List '。有些方法可以像[這裏的這個問題]中指出的那樣做(http://stackoverflow.com/questions/39406505/serializing-anonymous-types),但它們似乎不被推薦。 – granit