Skip to main content

Example on Using LINQ in code and LINQ Resources

Using LINQ with JSON, XML, and other datasets

Ossi Galkin avatar
Written by Ossi Galkin
Updated over a year ago

Using LINQ in code

LINQ (Language Integrated Query) is a uniform query syntax in C# used to retrieve data from different sources and formats. LINQ can be used with objects like JSON, XML, and other datasets. LINQ can be written in SQL, like in query expressions, or in more concise lambda expressions. This tutorial will use query expressions.

The example in the course utilizes LINQ. However, the tutorial does not teach you LINQ itself. The focus of the course is introducing the usages of the Code Element. The example material is from a Newtonsoft guide. The example creates list of titles of blog posts from JSON data.

Before going into the example in more detail, here are a couple of points for you to note:

  • Even though Frends offers you a possibility to write your own code, the first priority is to utilize pre-defined Tasks from the Task library.

  • When implementing C# code into Processes, you should consider creating a new custom Task in the case that the C# code grows in terms of the amount of code or the complexity of the code.

  • If the created C# code is duplicated into multiple Processes the maintenance of the code will be cumbersome.

Example Process:

The example Process itself is pretty simple and straightforward. It contains, in addition to Trigger and Result Elements, three Tasks.

Study the Task configurations from the description below. In case you have a Frends Environment in use, it is recommended that you implement the Process as you study the course content.

Process Task configurations

The 'Assign JSON' Task stores JSON data in a variable named json. Later in the Process this variable is referred to with the #var.json reference. In a real-life solution, the JSON data most likely would be a result of, for example, an API call.

You can copy the JSON data to your own Process from here:

@"{ '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', 'categories': [ 'Json.NET', 'CodePlex' ] }, { 'title': 'LINQ to JSON beta', 'description': 'Announcing LINQ to JSON', 'link': 'http://james.newtonking.com/projects/json-net.aspx', 'categories': [ 'Json.NET', 'LINQ' ] } ] } }"

The next Task, Convert json to JToken, does what the Task's name says: converts the data to JToken.

The last Task in the Process is named ‘Assign titles’ which is a C# Element type. The code in the Element configuration is a LINQ query. The query creates a list of blog titles from JSON data. The returned result of the C# code is stored to a variable named titles.

Copy the code from below to your own Process.

{ var postTitles = from p in ((JObject)#result[Convert json to JToken].Jtoken)["channel"]["item"] select (string)p["title"]; return postTitles; }

Finally, the Return Element is set to return #var.titles.

Process execution result

Now you can save and run the Process and result should be as follows:

LINQ resources

You can find more information about the LINQ from the sources below:


Did this answer your question?