LINQ - SPLessons
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

LINQ Objects

LINQ to Objects

 

shape Introduction

This chapter demonstrates about the LINQ to Objects, which is used to access the in-memory data collections in the case of LINQ to SQL or LINQ to XML by using LINQ query supporting IEnumerable<T>. Following are the concepts are covered in this chapter.
  • LINQ to Objects introduction

LINQ to Objects introduction

shape Description

The expression "LINQ to Objects"  means the utilisation of LINQ queries with IEnumerable<T> accumulation directly i.e no need to use any middle LINQ supplier or API for example, LINQ to SQL or LINQ to XML. User can utilise LINQ to question or enumerable accumulations. In a fundamental sense, LINQ to Objects speaks to another way to deal with accumulations. In the old procedure, user need to compose complex foreach circles which predefined how to recover information from an accumulation. LINQ to objects over a foreach loop have the many more advantages like powerful filtering, readability, capability of grouping and enhanced ordering with the minimal application coding. LINQ queries are no need to modify if need user can use with just little modifications. The code below demonstrates the LINQ to Object. [csharp] using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace LINQtoObjects { class Program { static void Main(string[] args) { string[] tools = { "Antony", "Dany", "Tony", "Cooper", "John", "David" }; var list = from t in tools select t; StringBuilder sb = new StringBuilder(); foreach (string s in list) { sb.Append(s + Environment.NewLine); } Console.WriteLine(sb.ToString(), "Tools"); Console.ReadLine(); } } } [/csharp] Result Compiling and execute the above code in a Visual Studio then user can get the output is a shown below. The code below demonstrates the querying a memory collection by using LINQ to the object is as shown below. [csharp] using System; using System.Collections.Generic; using System.Linq; namespace LINQtoObjects { class Department { public int DepartmentId { get; set; } public string Name { get; set; } } class LinqToObjects { static void Main(string[] args) { List<Department> departments = new List<Department>(); departments.Add(new Department { DepartmentId = 1, Name = "Finance" }); departments.Add(new Department { DepartmentId = 2, Name = "Sales" }); departments.Add(new Department { DepartmentId = 3, Name = "Marketing" }); var departmentList = from d in departments select d; foreach (var dept in departmentList) { Console.WriteLine("Department Id = {0} , Department Name = {1}", dept.DepartmentId, dept.Name); } Console.WriteLine("\nPress any key to continue."); Console.ReadKey(); } } } [/csharp] Result Compiling and execute the above code in a Visual Studio then user can get the output is a shown below. The code below demonstrates the querying a memory collection by using LINQ to the object in VB is as shown below. [csharp] Imports System.Collections.Generic Imports System.Linq Module Module1 Sub Main(ByVal args As String()) Dim account As New Department With {.Name = "Finance", .DepartmentId = 1} Dim sales As New Department With {.Name = "Sales", .DepartmentId = 2} Dim marketing As New Department With {.Name = "Marketing", .DepartmentId = 3} Dim departments As New System.Collections.Generic.List(Of Department)(New Department() {account, sales, marketing}) Dim departmentList = From d In departments For Each dept In departmentList Console.WriteLine("Department Id = {0} , Department Name = {1}", dept.DepartmentId, dept.Name) Next Console.WriteLine(vbLf & "Press any key to continue.") Console.ReadKey() End Sub Class Department Public Property Name As String Public Property DepartmentId As Integer End Class End Module [/csharp] Result Compiling and execute the above code in a Visual Studio then user can get the output is a shown below.

LINQ and Strings

shape Description

LINQ is  used as a query and used to change strings or group of strings. Which is  particularly helpful to the semi-organized information in content of documents. LINQ queries are consolidated with the conventional string capacities and regular expressions. For instance, User can utilise the Split or Split technique to make a variety of strings by utilising LINQ. User can utilise the IsMatch method in which where clause is a LINQ question and also user can utilise LINQ to inquiry or alter the MatchCollection results which are returned by a regular expression.

LINQ and Reflection

shape Description

.NET Framework class is a  library reflection APIs which can be utilised to analyse the metadata in a .NET get together and makes accumulations of sorts, sort individuals, parameters. Since these accumulations support non specific IEnumerable interface, and which can be questioned by LINQ.

Summary

shape Points

  • LINQ variable returns IEnumaerable type variables.
  • LINQ to object offers fresh collection approach.
  • LINQ queries have many advantages over foreach loops.