BITE SIZE C# – LINQ

LINQ or Language Integrated Query is part of the Microsoft .NET Framework and it adds native data querying capabilities to .NET languages.

LINQ allows the user to query:

  • Objects in Memory (i.e. collections such as lists) using LINQ to Objects
  • Databases using LINQ to Entities
  • XML using LINQ to XML
  • ADO.Net Datasets using LINQ to Datasets

LINQ can either be implemented using predefined extension methods or alternatively using LINQ Query Operators.

Below are three examples utilising LINQ Extension methods:

In this example a list of Book objects is filtered to return only the Objects where the price is less than 10.

List<Book> cheapBooks = books.Where(b=>b.Price < 10);

 

In this example a list of Book objects is filtered to return only the Objects where the price is less than 10 and additionally the newly created list of Book objects is sorted alphabetically based on the Title field.

List<Book> cheapBooks = books.Where(b=>b.Price < 10).OrderBy(b.Title);

 

In this example the cheapBooks list is filtered to only return the Titles of the books therein and these Titles are then inserted into a new list of strings.

List<string> cheapBooksTitles = cheapBooks.Select(b=>b.Title);

 

Multiple extension methods can be combined to ascertain the desired results, for example:

List<string> cheapBooksTitles = books.Where(b=>b.Price).OrderBy(b.Title);
                                   .Select(b=>b.Title);

 

LINQ query operators tend to be slightly more verbose, and the above example can be implemented with query operators as follows:

List<string> cheapBooksTitles = from b in books
                         where b.Price < 10
                         orderby b.Title
                         select b.Title;

 

Some common extension methods are:

Single

var book = books.Single(b=>b.Title == ”Building Robots”);

Returns a single object that matches the defined criteria. However note that in the event that none or more than one book matches the criteria specified an exception will be thrown.

 

SingleOrDefault

var book = books.SingleOrDefault(b=>b.Title == ”Building Robots”);

Returns a single object that matches the defined criteria, if more than one book matches the criteria specified an exception will be thrown, however if no books match the criteria the default value defined will be returned.

 

First

Returns the first object that matches the criteria, however if no matches are found an exception is thrown.

 

FirstOrDefault

Returns the first object that matches the criteria and if no match is found the default value will be returned.

 

Last

Returns the last object that matches the criteria, however if no matches are found an exception is thrown.

 

LastOrDefault

Returns the last object that matches the criteria and if no match is found the default value will be returned.

 

Max

var maxValue = books.Max(b=>b.Price);

Max is used with numeric values and will return the highest value that is contained in the Price field in the Book objects.

 

Min

var minValue = books.Min(b=>b.Price);

Min is another operation used with numeric values and will return the smallest value that is contained in the Price field in the Book objects.

 

Sum

var totalPrice = books.Sum(b=>b.Price);

Sum is used to add up all the values in a numeric field.

 

There are many other methods available to filter and manipulate data in LINQ and the possibilities for the utilisation of LINQ are nearly endless, for example

var bookSelection = books.Skip(2).Take(3);

The above example will skip the first two books in the books list and take the next three placing them into the newly created bookSelection list.

The best option to gain a better insight of what is possible with LINQ is to give it a try.

BITE SIZE C# – LINQ

BITE SIZE C# – LAMBDA EXPRESSIONS

A lambda expression is a function definition with no name, no access modifier and no return statement. Lambda expressions are also known as Anonymous methods or functions.

The syntax for the definition of a lambda expressions is as follows:

(arguments) => expression

The below C# code does not use lambda expressions and is given as a comparative reference point:

static int SquareNumber(int number)
{
   return number*number;
}

static void Main(string[] args)
{
   Int y = SquareNumber(10);
   Console.WriteLine(y); // This will display 100
}

The above code can be refactored using a lambda expression as follows:

static void Main(string[] args)
{

   Func<int,int> square = number=>number*number;
   Console.WriteLine(square(10)); // This will display 100

}

The result of this refactor is fewer lines of code in order to achieve the same result.

Lambda expressions can also make use of Delegates as shows here:

delegate int squareDelegate (int number);

static void Main(string[] args)
{
   squareDelegate square = number=>number*number;
   Console.WriteLine(square(10)); // This will display 100
}

 

The syntax for a lambda expression that takes no arguments is as follows:

()=>expression

For one argument:

X=>expression

For more than one argument:

(x,y,z)=>expression

 

Lambda expressions can also be very effectively used with objects that implement the IEnumerable and IQueryable interfaces to filter or search based on defined criteria, for example:

var highPriceList = PriceList.FindAll(a=>a.Price>10);

This will return all items with a price larger than 10, and place these items in the highPriceList.

 

var nyCustomers = Customers.Where(a=>a.City==”New York”);

This will place all customers with the city property set to “New York” into the nyCustomers collections.

Lambda expressions are an extremely useful tool with many other uses.

BITE SIZE C# – LAMBDA EXPRESSIONS

REVIEW – AMAZON FIRE 10 HD (7TH GENERATION)

The Fire 10 HD is a tablet computer developed by Android. The one we are looking at today is the most recent iteration of this tablet, the 7th generation which was released in 2017.

The Fire 10 HD is a 10.1-inch tablet with a screen to body ration of approximately 71%. The screen is a 1920 x 1200 IPS LCD panel with a 16:10 screen ration and a screen density of 224 ppi. The screen is by far the best part of this tablet, it is bright, crisp and has a very large viewing angle.

 The Fire 10 HD has a Quad-core processor consisting of two 1.8GHz Cortex-A72 cores and two 1.4 GHz Cortex-A53 cores, making the tablet feel snappy and responsive. For a GPU the tablet uses the PowerVR G6250. The Fire 10 has 2 GB of RAM and comes in two variations for storage 32 and 64 GB but both can accept SD cards of up to 256GB.

The tablet has a VGA front-facing camera and a 2MP rear-facing camera which is capable of 720p video recording. The cameras are definitely the weak point of this tablet and to be frank they terrible to the point of being unusable. However, I have never actually used the camera functionality on any tablet I have owned so this does not really bother me.

The Fire 10 has a 3.5 mm stereo jack and the integrated dual stereo speakers implement Dolby Atmos Audio and they sound great, you can comfortably watch a movie without using headphones.

From a connectivity perspective the Fire 10 has dual-band Wi-Fi and built-in Bluetooth. The tablet has a micro USB connector used for charging the battery and data transfer.

The battery is advertised to last up to 10 hours and after 4 months of daily use I typically get 7-8 hours of usage between charges.

The tablet comes in three color options, red, blue and black and weighs in at around 500 grams.

fireback

A customized version of Android, called Fire OS, is used by Amazon on the Fire product range. This means that the default App store for the device is the Amazon App Store, however the Google Play Store can be easily installed to get access to the entire Android app library.

This device is great for content consumption, and this is predominantly what I use it for. From Amazon Prime video, to Kindle Books and Comic books, to Audible Audio Books, to Magazines, to Podcasts this tablet does an exceptional job at offering a convenient way to get access to a vast variety of content.

Since acquiring this tablet, I read significantly more comic books and magazines as I can easily and relatively inexpensively get access to them.

Now given all this, the real surprise of the Fire 10 HD is the price, coming in at $150(USD) if you opt for the Special Offer option, which means ads will be displayed on the lock screen of the device, or alternatively $15(USD) more to remove the ads. And given that the cheapest variation of the latest iPad is over $300 at present, this tablet offers exceptional value.

From a content consumption perspective, the Fire 10 HD is faultless. With access to the entire Amazon library of content, the selection is endless. So, if you are looking for a cheaper alternative to the iPad with a well-established ecosystem, or you are just looking for a convenient way to read your electronic books, comics and magazine the Amazon Fire 10 HD is a perfect choice.

REVIEW – AMAZON FIRE 10 HD (7TH GENERATION)

BOOK REVIEW – BATMAN: THE COURT OF OWLS AND BATMAN: THE CITY OF OWLS

The Court of Owls written by Scott Snyder and The City of Owls written by Scott Snyder and James Tynion IV, formed part of the New 52 line and was originally published from 2011 to 2012.

The main story focuses on mysterious secret society, The Court of Owls, operating in Gotham city who attempt to assassinate various high-profile individuals, including Bruce Wayne, using a group of highly trained assassins called the Talons.

The story does a great job of building a sense of history, covering several members of past generations of the Wayne Family and even a story focused on Alfred’s father, who served as a butler to Thomas and Martha Wayne when Bruce was a small child.

Gotham City is also fleshed out more than any other Batman story, making it feel like an important character in the story, not just a location therein.

Without spoiling anything, the story progresses at an amazing pace, building tension and mystery at just the right amount, making it a very difficult book to put down.

This story arc was the start of the New 52 Batman reboot and is a classic Batman story with great art work that pulls the reader in. It is a great place to start for people new to the Batman comic books series and is a must read for any fan of the Batman comic.

BOOK REVIEW – BATMAN: THE COURT OF OWLS AND BATMAN: THE CITY OF OWLS

REVIEW – ADATA SD600 EXTERNAL SSD (256GB)

I recently found myself in the market for a high-capacity high-speed external storage solution and after shopping around I decided to pick up the ADATA SD600 External Solid State Drive as it provided 256GB of storage at a very reasonable price of just under $75 (USD).

The SD600 is a USB 3.1 compatible device, advertising read speeds of up to 440MB/s, very fast compared to more traditional external USB hard drives.

The SD600 utilizes 3D NAND technology, thus offering better performance compared to Solid State Drives that does not.

Below is a performance comparison, using Crystal Disk Mark, of the ADATA SD600, a Samsung EVO 850 500GB internal SSD running on SATA III and a SanDisk Ultra Flair 16GB USB 3.0 Thumb Drive:

ADATA

ADATA SD600 Results

 

Samsung evo 850 Crystal Disk

Samsung EVO 850 Results (SATA III)

 

USB3 Crystal Disk

SanDisk Ultra Flair 16GB USB 3.0 Thumb Drive Results

As can be seen the ADATA SD600 performs much better than the USB 3.0 thumb drive, but does not quite match the results of the Samsung drive running on SATA III. However, for an external storage solution these results are great.

From a size perspective the SD600 is much smaller than a traditional 2.5-inch external Hard Drive and the image below shows the size compared to two USB thumb drives.

ADATA Size

The SD600, however slightly larger than the thumb drives, is really compact and is definitely small enough to be comfortably carried around in your pocket. It is also very durably built and offers a very convenient solution for portable storage.  Thus far, after over a months’ worth of usage the SD600 has given me no problems and serves its purpose exceptionally well. So, if you are in the market for an external storage solution the SD600 offers a great solution at a very reasonable price.

REVIEW – ADATA SD600 EXTERNAL SSD (256GB)

BOOK REVIEW – BATMAN HUSH

hush

Batman Hush is a mainline Batman story arc that was originally published in the Batman Comic between 2002 to 2003, which has subsequently been published in this graphic novel. Hush is a great self-contained story which can easily be picked up by anyone looking for a starting point in the almost endless selection of Batman comics.

Batman Hush is one of the best usages of the Batman Villains’ gallery, featuring a large cast including, the Riddler, Killer Croc, Poison Ivy, Scarecrow and the Joker to name a few. The graphic novel tells a great story written by Jeph Loeb, full of twists and a few surprises along the way, it really is one of the classic must read story in the Batman comic series.

The story is beautifully bought to life by the artwork, which is some of my favorite in the long running Batman comic series. The art is the combined work of Jim Lee, Scott Williams, and Alex Sinclair, being responsible for pencil, ink and color respectively.

I can really recommend this graphic novel if you want to get started in the Batman comic series as it is a great jumping-off point and if you are already reading the series and have not read Hush, then you are really missing out.

BOOK REVIEW – BATMAN HUSH

BOOK REVIEW – LITTLE NIGHTMARES VOLUME 1 GRAPHIC NOVEL

IMG_6351

Needless to say, this graphic novel is based on the game Little Nightmares and contains the first 2 comic books in the series. The series was initially announced to consist of 4 comic books, however at the time of this post book 3 and 4 unfortunately appear to have been cancelled.

Anyone who has played Little Nightmares will know that the world of the game is a dark and mysterious place, with a lot of unanswered questions. This graphic novel builds on this world and provides some additional bits of information about the greater world that was not directly shown in the game.

I really like the ascetics and feel of Little Nightmares and part of its appeal is this sense of unknown, and the graphic novel does not spoil this, providing just enough information to get the reader more curious. For example, the comic books explain how the kids end up on The Maw, the fictional ship on which the game and its DLC takes place, but gives no additional details about the mysteries surrounding the Maw itself.

The graphic novel is beautifully illustrated with amazing artwork, in line with the graphic style of the game.

It is however important to mention that this graphic novel will not make sense to someone who has not played the game. It is very much a companion piece to the game and builds on the world that was established therein. But if you played Little Nightmares and loved the world it built then this will be a great read.

BOOK REVIEW – LITTLE NIGHTMARES VOLUME 1 GRAPHIC NOVEL