我正試圖將一個平面資料源投射到一個可以使用Newtonsoft.Json直接序列化為JSON的物件。我在Linqpad中創建了一個小程式,用一個想象中的庫存概覽作為測驗。要求的輸出如下:
- 網站名稱
- 存貨名稱
- 產品名稱
- 重量
- 單位
- 產品(等)
對于我來說,我無法讓它只擁有一個 "網站名稱 "作為唯一的根物件。我想讓它列出一個網站內的庫存內容,但它最終總是看起來像。

我怎樣才能使 "網站 "具有不同的 "庫存 "集合,而每個庫存都有一個 "產品 "集合?
我的實際資料源是一個資料庫表,它類似于我的測驗物件的結構--它就是這樣的。
Linqpad 中的測驗代碼。(注意,它參考了 Newtonsoft.Json) JSON輸出: 建議的正確輸出示例: uj5u.com熱心網友回復: 好吧,我有一個使用字典的解決方案,它將正確地分組所有內容: 我有一個使用字典的解決方案。
這是輸出json: 正如你所看到的,我按網站分組,然后將其變成一個字典,然后我按庫存分組,并將其變成另一個字典,并將產品串列作為值。
因此,基本上結果是 字典中的鍵是Site、Inventory Name。 我每次都使用這個GroupBy-->>字典來建立像這樣的分層json。
標籤:void Main()>
{
var contents = new List<DatabaseRecord> ()
{
new DatabaseRecord{Product="Autoblaster", Inventory="Hull 5" , Site="死亡之星", Units=20,weight=500},
new DatabaseRecord{Product="E11 Blaster Rifle", Inventory="Hull 5" /span>, 地點="死亡之星", 單位=512,重量=4096}。
new DatabaseRecord{Product="SWE/2 Sonic Rifle"/span>, Inventory="Hull 1"/span>, Site="死亡之星", Units=20,weight=500},
new DatabaseRecord{Product="Relby v10 Micro Grenade Launcher"/span>, Inventory="Hull 5"/span>, Site="死亡之星", Units=20,weight=500},
new DatabaseRecord{Product="T-8 Disruptor"/span>, Inventory="Hull 1"/span>, Site="死亡之星", Units=20,weight=500},
new DatabaseRecord{Product="E11 Blaster Rifle", Inventory="Hull 2" , Site="死亡之星", Units=50,Weight=1200}.
};
var inventorycontent = from 行 in contents
group row by row.Site in into sites
orderby sites.Key
select from inventory in sites
group inventory by inventory.Inventory in into inventories
orderby inventories.Key
選擇 新
{
site = sites.Key,
inventory = inventories.Key,
lines = inventories.Select(i => new { i.產品, i.重量, i.單位 })
};
contents.Dump()。
inventorycontent.Dump()。
JsonConvert.SerializeObject( inventorycontent, Newtonsoft.Json.Formatting.Indented).Dump()。
}
//在這里定義其他方法和類。
class DatabaseRecord
{
public string Product { get; set; }
public string Inventory { get; set; }
public string Site { get; set; }
public int Units { get; set; }
public double Weight { get; set; }
[
[
{
"site"。"死亡之星"。
"庫存": "Hull 1",
"線路": [...
{
"Product": "SWE/2 Sonic Rifle"。
"重量": 500.0,
"單位": 20.
},
{
"產品": "T-8 Disruptor",
"重量": 500.0,
"單位": 20.
}
]
},
{
"site": "死亡之星"。
"庫存": "Hull 2",
"線路": [...
{
"Product": "E11爆破步槍"。
"重量": 1200.0,
"單位": 50.
}
]
},
{
"site": "死亡之星"。
"庫存": "Hull 5",
"線路": [...
{
"Product": "Autoblaster",
"重量": 500.0,
"單位": 20.
},
{
"產品": "E11爆破步槍",
"重量": 4096.0,
"單位": 512.
},
{
"產品": "Relby v10 Micro Grenade Launcher",
"重量": 500.0,
"單位": 20.
}
]
}
]
]
{
"sites": [{
"site": "死亡之星"。
"庫存":[
{
"name": "Hull 1",
"lines":[{
"產品": "SWE/2 Sonic Rifle"。
"重量": 500.0,
"單位": 20.
},
{
"產品": "T-8 Disruptor",
"重量": 500.0,
"單位": 20.
}]
},
{
"name":"Hull 2" ,
"線條":[{
"產品": "SWE/2 Sonic Rifle"。
"重量": 500.0,
"單位": 20.
}]
}
]
},
{"site": "其他站點"。
"存貨":[
{
"name":"Hull 1",
"lines":[{
"產品": "SWE/2 Sonic Rifle"。
"重量": 500.0,
"單位": 20.
}]
}]
/first get everything properly grouped with dictionaries
var result = contents
.GroupBy(x => x.Site)
.ToDictionary(g => g.Key, g => g
.GroupBy(i => i.Inventory)
.ToDictionary(i => i.Key, i => i
.Select(a => new)
{
Product = a.Product,
重量 = a.Weight,
單位 = a.單位
})
.ToList())。
//project to a new object that matches your desired json
var formattedResult = new
{
sites = (from r in result
選擇 新
{
site = r.Key,
inventories = (from i in r.Value select new { name = i.Key, lines = i.Value }) .ToList()
}).ToList()
};
{
"sites": [
{
"site": "死亡之星"。
"存貨": [
{
"name": "Hull 5",
"lines": [
{
"Product": "Autoblaster",
"重量": 500.0,
"單位": 20.
},
{
"產品": "E11爆破步槍",
"重量": 4096.0,
"單位": 512.
},
{
"產品": "Relby v10 Micro Grenade Launcher",
"重量": 500.0,
"單位": 20.
}
]
},
{
"name": "Hull 1",
"線路": [
{
"Product": "SWE/2 Sonic Rifle"。
"重量": 500.0,
"單位": 20.
},
{
"產品": "T-8 Disruptor",
"重量": 500.0,
"單位": 20.
}
]
},
{
"name": "Hull 2",
"線路": [
{
"Product": "E11爆破步槍"。
"重量": 1200.0,
"單位": 50.
}
]
}
]
}
]
}
Dictionary<string, Dictionary<string, List<Products>>
