目前,我使用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
但它似乎並沒有工作。任何答覆或幫助將不勝感激。提前致謝。
爲什麼要將其轉換爲字節數組?客戶如何設想理解它? –