2010-10-23

[Tips] Entity Framework and JSON

需求

提供JSON的格式資料,但是不要將所有的entity欄位都輸出.

 

方式

 

輸出所有欄位

   1: using (MyStockEntities entities = new MyStockEntities())
   2: {
   3:     JavaScriptSerializer js = new JavaScriptSerializer();
   4:     return js.Serialize(entities.CodeStocks);
   5: }

 


輸出需要的欄位



   1: using (MyStockEntities entities = new MyStockEntities())
   2: {
   3:     JavaScriptSerializer js = new 
   4:     JavaScriptSerializer();
   5:     var results = from cols in entities.CodeStocks
   6:         select new
   7:         {
   8:             StockId = cols.StockId,
   9:             StockName = cols.StockName,
  10:             StockType = cols.StockType
  11:         };
  12:     return js.Serialize(results);
  13: }

Ubunutu 上安裝python2, 3, pip 跟virtualenv

# 安裝 python2, python3 (ubuntu 17.04已經預設安裝python3, 但沒有python2) sudo apt install python python-dev python3 python3-dev # 安裝 pip curl https://b...