Skip to content

Instantly share code, notes, and snippets.

@rupertbates
Created January 14, 2010 13:21
Show Gist options
  • Save rupertbates/277153 to your computer and use it in GitHub Desktop.
Save rupertbates/277153 to your computer and use it in GitHub Desktop.
Open Platform blog code snippets
//Get all users over the ages of 18
var adults = users.Where(u => u.Age > 18);
//Get 20 users over the age of 18, ordered
//by name, skipping the first 10
var someAdults = users
.Where(u => u.Age > 18)
.OrderBy(u => u.Name)
.Skip(10)
.Take(20);
//Get a breakdown of number of users by department
var usersByDepartment = users
.GroupBy(u => u.Department) //group the users by department
.Select(g => new{
Department = g.Key,
Count = g.Count()
});
//Run a simple content search
var op = new OpenPlatformSearch();
var searchParams = new ContentSearchParameters {Query = "Fats Domino"};
var results = op.Search(searchParams);
foreach(Content c in results.Results)
{
Debug.WriteLine(c.ApiUrl);
}
//Run a simple tag search
var tagParams = new TagSearchParameters {Query="music"};
var tags = op.Tags(tagParams);
foreach (Tag t in tags.Tags)
{
Debug.WriteLine(t.Type);
}
//Fetch an item of content by id
var item = op.Item(344089365);
Debug.Write(item.Headline);
//each piece of content has a collection of tags
foreach (Tag t in item.TaggedWith)
{
Debug.WriteLine(t.Name);
}
var op = new OpenPlatformSearch();
var date = DateTime.Today.AddDays(-7.0);
var results = op.Search(new ContentSearchParameters
{
After = date,
Count = 100, //there will be fewer results than this
//we just want to bring them all back
Filters = new List<string>
{
"/music"
}
});
//These are the tags which match with music types
Tag PopAndRock=new Tag{Filter="/music/popandrock"};
Tag Classical=new Tag{Filter="/music/classicalmusicandopera"};
Tag Electronic=new Tag{Filter="/music/electronicmusic"};
Tag Urban=new Tag{Filter="/music/urban"};
Tag Folk=new Tag{Filter="/music/folk"};
Tag WorldMusic=new Tag{Filter="/music/worldmusic"};
Tag[] MusicTypes=new[]
{
PopAndRock, Classical, Electronic, Urban, Folk, WorldMusic
};
//filter the results to get the ones which
//are tagged with the musicType tags
var filtered=results.Results
.Where(c => c.TaggedWith
.Intersect(MusicTypes).Any());
//group the results by day with a
//new collection for each music type
var grouped = filtered.GroupBy(c => c.PublicationDate)
.OrderBy(g => g.Key) //g.Key is the publication date, sort by this
.Select(g => new MusicModel //transform the results
{
Day = g.Key,
PopAndRock = g.Where(c => c.TaggedWith.Contains(PopAndRock)), //The Pop and Rock articles
Classical = g.Where(c => c.TaggedWith.Contains(Classical)),
Electronic = g.Where(c => c.TaggedWith.Contains(Electronic)),
Urban = g.Where(c => c.TaggedWith.Contains(Urban)),
Folk = g.Where(c => c.TaggedWith.Contains(Folk)),
WorldMusic = g.Where(c => c.TaggedWith.Contains(WorldMusic)),
All = g
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment