DAT BLOG POST 11
- daniel-ewers2
- Jun 20, 2021
- 2 min read
Sessions 16-17 – LINQ
What Have I Learned?
This week we covered LINQ. I had already learned this prior to this class as I had used it in another class’s assignment. LINQ (Language Integrated Query) is a unified query syntax for retrieving data from many sources and formats in C# and VB.NET. It's integrated in C# and Visual Basic, removing the gap between programming languages and databases while also offering a single querying interface for many data sources (Tutorialsteacher, n.d.).
Why Have I Learned This?
LINQ is a sophisticated query language that may be used in a number of circumstances. When creating apps, there are numerous techniques we may employ to accomplish things, but some of them are patchwork, resulting in code that is longer than it has to be. It is preferable to employ a declarative high-level approach that explains what we want rather than elaborating on how we will achieve it. The great thing about LINQ is that it can be implemented in the same way regardless of the suitable data source you use.
LINQ provides us with a variety of benefits such as being a first-class language construct (like classes and methods) and is a consistent experience across objects (such as lists and arrays), relational databases (such as MYSQL), and XML.
Below is an example of LINQ that I have implemented into my code:

This changes the data source of a datagridview to a list of vehicle objects from a business's vehicle list and sorts the list in ascending order by car registration. This technique is considerably more readable and succinct than using a foreach loop to read over the list and add items to the datagridview. I noticed that, while a for loop appears to function, it really generates a duplicate of the data rather than directly referencing the objects in the list. This indicates that you're refining the data copies, which means that any changes you make to the data won't affect the parent list.
How Have I Learned This?
I have learned this by doing some research into what LINQ is and how I implement them into my code. I practised it many times and implemented it in as many scenarios as I could to ensure that my code was not bloated with unnecessary code. There were many situations where I found myself writing endless lines of code that was easily refined using LINQ. There are many tutorials and references to how to use LINQ online that have also helped me implement LINQ into my applications.



Comments