2015-05-19 198 views
0

我有這樣的對象列表列表:組對象的以JSON數組

CreateDate Name  Unit 
2015-01-01 Users  100 
2015-01-01 Items  50 
2015-01-02 Users  400 
2015-01-02 Items  150 

現在我想創建一個JSON數組,其中我集團在列表中的項目由CREATEDATE並獲得這樣的:

[ 
    {"CreateDate":"2015-01-01", "Users":100, "Items" : 50}, 
    {"CreateDate":"2015-01-02", "Users":400, "Items" : 150} 
] 

CreateDate和Name是數據庫中的PK,包含更多類型的名稱。

我有這樣的:

public class Statistics 
    { 
     public DateTime CreateDate { get; set; } 

     public int Unit { get; set; } 

     public string Name { get; set; } 
    } 

    [HttpGet] 
    public ActionResult Items(DateTime startDate, DateTime endDate) 
    { 
     try 
     { 
      List<Statistics> Coll = FROM DB 

      var Data = Coll.GroupBy(p => p.CreateDate); 

      return Json(Coll, JsonRequestBehavior.AllowGet); 
     } 
     catch (Exception Ex) 
     { 
      return ... 
     } 
    } 

什麼是做到這一點的最好方法是什麼?

+0

什麼是你與你的'Data'變量在做什麼?你的代碼是否工作,你只是在尋找更好的方法?或者你有一些錯誤? – CoderDennis

+0

數據只是一個測試,但Coll包含我上面寫的列表。 – mrcode

+0

因此,您的方法不會返回分組結果。像'GroupBy'這樣的LINQ擴展方法不會修改源'IEnumerable' – CoderDennis

回答

2
using Newtonsoft.Json; 
using System; 
using System.Collections.Generic; 
using System.Dynamic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication6 
{ 

    public class Transactions 
    { 
     public string CreateDate { get; set; } 
     public string Name { get; set; } 
     public int Unit { get; set; } 
    } 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      var transactionsList = new List<Transactions>(); 
      transactionsList.Add(new Transactions() { CreateDate = "2015-01-01", Name = "Users", Unit = 100 }); 
      transactionsList.Add(new Transactions() { CreateDate = "2015-01-01", Name = "Items", Unit = 50 }); 
      transactionsList.Add(new Transactions() { CreateDate = "2015-01-02", Name = "Users", Unit = 400 }); 
      transactionsList.Add(new Transactions() { CreateDate = "2015-01-02", Name = "Items", Unit = 150 }); 
      transactionsList.Add(new Transactions() { CreateDate = "2015-01-02", Name = "NewItems", Unit = 600 }); 

      var dictionaryList = new List<Dictionary<string, object>>(); 

      var transactionsQuery = (from t in transactionsList 
            group t by new { t.CreateDate, t.Name } 
             into grp 
             select new 
             { 
              grp.Key.CreateDate, 
              grp.Key.Name, 
              Units = grp.Sum(t => t.Unit) 
             }).ToList(); 

      var days = (from t in transactionsList 
         group t by new { t.CreateDate } 
          into grp 
          select grp.Key.CreateDate).ToList(); 


      foreach (string day in days) 
      { 
       var dataOfDay = new Dictionary<string, object>(); 
       dataOfDay.Add("CreateDate", day); 
       foreach (var transaction in transactionsQuery.Where(x => x.CreateDate == day)) 
       { 
        dataOfDay.Add(transaction.Name, transaction.Units); 
       } 
       dictionaryList.Add(dataOfDay); 
      } 



      Console.WriteLine(JsonConvert.SerializeObject(dictionaryList)); 
      Console.ReadLine(); 

      // Output : 
      // [{"CreateDate":"2015-01-01","Users":100,"Items":50},{"CreateDate":"2015-01-02","Users":400,"Items":150,"NewItems":600}] 


     } 
    } 
} 
+0

Thanx。完美的作品! – mrcode

0

它看起來像你只需要重新塑造使用LINQ的數據。這將工作根據您提供的樣本數據:

var grouped = stats.GroupBy(x => x.CreateDate); 

var data = grouped.Select(g => new { 
    CreateDate = g.Key, 
    // you might need to adjust the First() depending on whether you have nulls or multiples in your data 
    Users = g.First(stat => stat.Name=="Users").Unit, 
    Items = g.First(stat => stat.Name=="Items").Unit 
}); 

編輯

關於它的思考多一些,如果你有多個用戶/項目的日期,或者如果你這個LINQ語句可能會更安全有,例如,一個用戶排了一個日期,但沒有條目行:

var result = grouped.Select(g => new { 
    CreateDate = g.Key, 
    // you might need to adjust the First() depending on whether you have nulls or multiples 
    Users = g.Where(stat => stat.Name=="Users").Sum(stat => stat.Unit), 
    Items = g.Where(stat => stat.Name=="Items").Sum(stat => stat.Unit) 
}); 

那麼你應該能夠吐出來,以JSON:

return Json(data, JsonRequestBehavior.AllowGet); 
+0

謝謝。但是,如果我在數據庫中添加新的項目,如名稱=「汽車」和名稱=「貓」,我還必須在代碼中添加它?如果可能,我希望它更具動感。 – mrcode