Skip to main content

Linq everything you need to know

 

Linq

Links

·         Linq cheat sheet to print http://nickberardi.com/linq-cheat-sheet/
·         Linq Immediate and Deferred execution (intermediate) http://www.dotnetcurry.com/linq/750/deferred-vs-immediate-query-execution-linq
·          


What is it for? What are the benefits? Do I need it?

Reduces code line numbers.
The Linq query can be passed around as a method parameter, it’s not actually run until the developer calls ToList() or something on the query

Linq is not required but it looks good.

Linq and Lambda

Linq is the queryable language, in C# this is implemented as two different types Extension methods (methods like Where() ) or Query Syntax (sql style).

Lambda is the part with the weird => notation known as “to”.
e.g. The complete expression can be known as a Linq query. Made up of a Linq extension method and a Lambda expression.
var allFirstsQuery = data.Where(d => d.Name == “First”);

From Stackoverflow: http://stackoverflow.com/questions/7391370/is-it-linq-or-lambda


This is LINQ (using query syntax):
var _Results = from item in _List
                where item.Value == 1
                select item;
This is also LINQ (using method syntax):
var _Results = _List.Where(x => x.Value == 1);
It's interesting to note that both of these flavors will end up producing the exact same code. The compiler offers you a service by allowing you to express your wishes in the manner that you prefer.
And this is a lambda:
x => x.Value == 1


How to use it

Replacing existing foreachs with linq.

Different types of Linq Methods

LINQ standard query operators (implemented with extension methods in C#) can be categorized into the following ones on the basis of their functionality. Filtering, Join, Projection, Sorting, Grouping, Conversions, Concatenation, Aggregation, Quantifier, Partition, Generation, Set, Equality, Element.

 

Common examples


More complex examples

Immediate and Delayed execution:

var allFirstsQuery = data.Where(d => d.Name == “First”);
When the runtime get to the line above it does not do anyting except create a variable result.
Only when the result is executed does the query actually run
var allFirstsResultAsList = allFirstsQuery.ToList();
So you could pass allFirstsQuery around as a normal object and execute later. The fact that a query can be created and later executed is important where the target will most likely be different at a later time e.g. the original data object could have a Database StoredProcedure call and the data in there could be different at a later time.


Comments

PoL said…
The Solve Care platform uses blockchain technology to reduce the huge global cost of clinical and information systems associated with our modern healthcare system.

Popular posts from this blog

dotNET - Debugging

Debugging with .NET MSIL assemblies Visual Studio and debugging the CLR are different, I'll talk about both. MSIL Assemblies Assemblies compiled with .NET tools such as the CLR compiler are compiled into a file which contains MSIL (Microsoft Intermediate Language). At runtime the contents of the assembly are loaded into the CLR and ran as machine code. When you compile an assembly in debug a PDB file is generated alongside the DLL or EXE you've just created. The link between these 2 files is that the PDB contains the line numbers of the methods and classes as well as the file names of the original source code that created the assembly. When you launch the debugger in Visual Studio the assembly is loaded into the Debugger (similar to the CLR) along with the PDB file. The debugger now uses your PDB file contents to match the running code found in the assembly to locations in source files (hopefully in your present project). CLR CLR Inside Out (msdn magazine) .NET Framework Tools:...

Installer CustomAction, Debugging the CustomAction, InstallState

Custom Action The Custom Action is added to the Setup Project, select the Project node and hit the Custom Action button. This allows you add an Action to a particular phase in the Installation. But first you must create the Custom Action. To Add a Custom Action you must first have a Custom Action created, this is usually in the form of a Installer Class, this should be created in a seperate project, the Installer Class is actually one of the File Templates in the C# Projects. So it's File->New Project and select Visual C# Projects. Then add a Class Library, this will prompt you for the Class Library Types , select "Installer Class". Walkthrough - Creating Custom Action (msdn). Also here's a more comprehensive document on Setup/Installer implementations, it delves into the Registry etc Getting Started with Setup Projects (SimpleTalk). Visual Studio Setup Projects and Custom Actions (Simple Talk). Create your Installer Class and then add it as a Custom Action to the ...

dotNET - Use app.config ApplicationSettings and UserSettings

When using Settings in an Assembly or .exe you can use the Settings Designer to generate a config file using Settings. The Settings Designer provides a wrapper class which allows you to provide defaults and access the config data using Properties. But what if you're not working inside that Assembly or .exe? this presents a problem. If your loading the Assembly externally and want to access that Assembly's .config file you'll probably wish to use something in the System.Configuration namespace... unfortunately it's not of much use if you've created the .config file from the Settings Designer in Visual Studio!! This is because the Designer creates Sections and ApplicationSettings and UserSettings, the System.Configuration namespace does not provide a method to access these (it has a method to access AppSettings which are a different thing. Below I've written a workaround which locates the app.config and accesses the ApplicationSettings and UserSettings using XML i...