-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathQueryJsonLinq.cs
More file actions
85 lines (74 loc) · 2.63 KB
/
Copy pathQueryJsonLinq.cs
File metadata and controls
85 lines (74 loc) · 2.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
// Copyright (c) 2007 James Newton-King. All rights reserved.
// Use of this source code is governed by The MIT License,
// as found in the license.md file.
// ReSharper disable PossibleMultipleEnumeration
public class QueryJsonLinq : TestFixtureBase
{
[Fact]
public void Example()
{
#region QueryJsonLinq
var json = """
{
'channel': {
'title': 'James Newton-King',
'link': 'http://james.newtonking.com',
'description': 'James Newton-King\'s blog.',
'item': [
{
'title': 'Json.NET 1.3 + New license + Now on CodePlex',
'description': 'Announcing the release of Json.NET 1.3, the MIT license and the source on CodePlex',
'link': 'http://james.newtonking.com/projects/json-net.aspx',
'category': [
'Json.NET',
'CodePlex'
]
},
{
'title': 'LINQ to JSON beta',
'description': 'Announcing LINQ to JSON',
'link': 'http://james.newtonking.com/projects/json-net.aspx',
'category': [
'Json.NET',
'LINQ'
]
}
]
}
}
""";
var rss = JObject.Parse(json);
var postTitles =
from p in rss["channel"]["item"]
select (string) p["title"];
foreach (var item in postTitles)
{
Console.WriteLine(item);
}
//LINQ to JSON beta
//Json.NET 1.3 + New license + Now on CodePlex
var categories =
(
from c in rss["channel"]["item"]
.Children()["category"]
.Values<string>()
group c by c
into g
orderby g.Count() descending
select new
{
Category = g.Key,
Count = g.Count()
})
.ToList();
foreach (var c in categories)
{
Console.WriteLine($"{c.Category} - Count: {c.Count}");
}
//Json.NET - Count: 2
//LINQ - Count: 1
//CodePlex - Count: 1
#endregion
Assert.Equal(3, categories.Count);
}
}