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)