preface

Remind: In order to be able to learn the knowledge more thoroughly, remember more firmly I will write down the knowledge by means of teaching on Because can let a person in the process from students to the teacher This process will be dug up new knowledge and ideas is a self thinking mining switch and a depth of knowledge and the process of ascension If you can help to everyone that is the best If there is a mistake, please give me more advice! I’m just a vegetable chicken thanks for understanding!


1. Get acquainted with LINQ

I’m sure you’ve read about LINQ on the Internet or in books, but what is it? Let’s learn about LINQ with you today

LINQ is a new feature integrated into the languages C# and VB.NET to provide the ability to query data

In a relational database, data is organized into the normative good table, and through the simple but powerful language SQL to access, process, however, in contrast to the database, stored in the data in a class object or structure difference is very big, so there is no general query to get the data from the data structure, the c # 3.0 after the introduction of LINQ, We then have the ability to query the object data

LINQ is C# that gives us developers the ability to quickly query data

LINQ———– makes the language more beautiful, makes the query more convenient, makes the code better.


2. Simple LINQ case

Let’s start with an example to show you the beauty of LINQ

To use LINQ, refer to the namespace in which it resides using System.linq;

using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class GameText { public int gameID; // Game ID public String gameName; // Public String GameInduced; // public int gameSize; } class Program {static List<GameText gamb0 game = new List<GameText gamb1 (){new GameText(){gameID = 1,gameName = 1 "Reduced ",gameIntduced =" It's a 2D internship interview project ",gameSize = 1}, New GameText(){gameID = 2,gameName = "You of my table ", gameInduced =" It's a youth schoolhouse game ",gameSize = 5},}; static void Main(string[] args) { var res = from m in game where m.gameID > 1 select m.gameName; foreach (var item in res) { Console.WriteLine(item); }}}}

It’s going to focus on this place

     var res = from m in game
          where m.gameID > 1
          select m.gameName;

From in are both keywords, and game is the object of the data that we’re querying. M stands for the elements inside the game

Where is the condition where I wrote a condition m.gameid > 1 which means to query for elements in the game set that have a gameID greater than 1

Select m. gamename finds the game that meets the criteria and returns its game name

Let’s look at the output

East gather together the west gather together the game ID is 1 with you ID is 2

where m.gameID > 1

Obviously you’re sitting in the same seat and this game qualifies so what we ended up with is you sitting in the same seat

See is not very simple, their own hands knock will be able to understand immediately!


3. Different Syntax on LINQ

This example is about the way LINQ expressions are written (also known as query syntax). Here we can also use method syntax to query data

The query syntax is declarative and looks very similar to SQL statements. Query syntax is written in the form of query expressions.

Method syntax is command-style and uses standard method calls. Methods are a set of methods called standard query operators

As long as we refer to System.linq; We can call the Where method

There is an Enumerable class in the System.linq namespace

The Enumerable class provides us as developers with a lot of extension methods and shared methods, which is quite a bit of water. We don’t want to skip over it, but we’ll just go straight to the collection and then. Just call the where method inside

It requires us to pass in a Func delegate and there are two ways of writing it

One way to do it is with Lambda expressions, and the other way is more cumbersome to write a separate method

So we’re going to have to write a method that takes GameText and returns bool to the place Where it’s going to do the filtering


//var res = from m in game
//          where m.gameID > 1
//          select m.gameName;

var res = game.Where(whereFunc);


foreach (var item in res)
{
    Console.WriteLine(item.gameName.ToString());
}


static bool whereFunc(GameText t)
{
    if (t.gameID > 1) return true;
    return false;
}

            

Start the program output is also the same seat you have nothing wrong with brother

Well, it’s not too much trouble to do just a few steps, but Lambda expressions make it a lot easier

//var res = from m in game // where m.gameID > 1 // select m.gameName; //var res = game.Where(whereFunc); var res = game.Where(g => g.gameID > 1); foreach (var item in res) { Console.WriteLine(item.gameName.ToString()); } //static bool whereFunc(GameText t) //{ // if (t.gameID > 1) return true; // return false; / /}

Or output the same seat you have no problem!

4. LINQ multi-table query operation

For those of you who have studied SQL, you are familiar with joins. I have only touched SQL for a while, and I am learning about joins for the first time. So what is it?

A join query takes two collections and creates a temporary collection of objects. In order for a join operation to take place, each source object must share a value that can be compared to determine equality

Go straight to the example and see the example

using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Gamer { public int id; public string playName; } class GameText { public string gameName; public int id; } class Program {static List<Gamer> game = new List<Gamer>() {new Gamer(){id = 0,playName = "playName "}, New Gamer(){id = 1,playName = "PlayName "}, new Gamer(){id = 2,playName =" PlayName "}, new Gamer(){id = 3,playName = "PlayName "}, New Gamer(){id = 4,playName = "small "},}; Static List<GameText gamb0 game2 = new List<GameText gamb1 (){new GameText(){gameName = "GameText ",id = 1}, New GameText(){gameName = "gameName ",id = 0}, new GameText(){gameName =" gameName ",id = 0}, New GameText(){gameName = "gameName ",id = 3},}; }}

Now we have two classes

Gametext stores the name of the game and the ID of the people who play it

Now we can simply search the game table to get the name and ID of the player, but we can not find out which game the player is playing. In this case, we can use the JOIN clause in LINQ.

static void Main(string[] args) { var res = from g in game join g2 in game2 on g.id equals g2.id where g2.gameName == Select new {playerName = g.playName, gameName = g2.gamename}; //var res = game.Where(g = >g.gameid > 1); foreach (var item in res) { Console.WriteLine(item); }}

The previous keywords have been explained, but here is the join operation

– join g2 in game2 means exactly the same as from g in game (note here that the query expression must start with the from clause)

From is the object to which the query is made. Join is the object to which the query is made. On is the join condition

Where g2.gamename == “where g2.gamename ==” where g2.gamename ==”

**Gamer stores the player’s ID and name. GameText stores the game’s name and the player’s ID**

They have the same ID and they are playing your game at the same seat so that satisfies the query condition

Go back to see if our watch is little red, little blue and little yellow these three people are playing the same seat you this game

Well, there’s nothing wrong with the output

There seems to be a joint query here, I did not understand the relationship between the two of them, the feeling is essentially the same can also be a multi-table query operation, waiting for which brother teach brother!

I’m also going to write out the joint query here and I don’t have to change anything else

Static void Main(String [] args) {var res = from G in game from G2 in game2 where G. id == G2.id && G2.gamename == ""  select new { playerName = g.playName, gameName = g2.gameName }; foreach (var item in res) { Console.WriteLine(item); }}

It shouldn’t be hard to understand without much explanation.


5. LINQ does sort operation on query results

The ORDERBY clause is introduced here

The orderby clause takes an expression and returns the result item based on the expression at once

The ORDERBY clause defaults to ascending ordering, and we can also explicitly set the ordering of the element to ascending or descending using the Ascending and descending keywords

Let’s take a look at an example

Just add OrderBy G.Id Descending and the rest of the code stays the same

using System; using System.Collections.Generic; using System.Linq; namespace ConsoleApp1 { class Gamer { public int id; public string playName; } class GameText { public string gameName; public int id; } class Program {static List<Gamer> game = new List<Gamer>() {new Gamer(){id = 0,playName = "playName "}, New Gamer(){id = 1,playName = "PlayName "}, new Gamer(){id = 2,playName =" PlayName "}, new Gamer(){id = 3,playName = "PlayName "}, New Gamer(){id = 4,playName = "small "},}; Static List<GameText gamb0 game2 = new List<GameText gamb1 (){new GameText(){gameName = "GameText ",id = 1}, New GameText(){gameName = "gameName ",id = 0}, new GameText(){gameName =" gameName ",id = 0}, New GameText(){gameName = "gameName ",id = 3},}; static void Main(string[] args) { var res = from g in game join g2 in game2 on g.id equals g2.id where g2.gameName == Select new {playerName = g.LayName, gameName = g2.gamename}; foreach (var item in res) { Console.WriteLine(item); }}}}

Because our default ID is 0, 1, 2, 3… So let’s put the data in reverse order and see what happens

In the ascending state, the output should be little red, little blue and little yellow. We use orderby g.id descending to output the results in reverse order according to g.id, so the output is little yellow, little blue and little red