LINQ Practice Tutorials

Source: Internet
Author: User

The purpose of this tutorial is to quickly master the basic usage of LINQ and Build complex LINQ queries on this basis, without any verbose or direct use.

Example 1: The simplest LINQ query

int[] numbers = {5, 1, 8, 6, 2, 6, 4, 9, 10, 7, 3};
var nums1 = from N in numbers where n < 5 select n;
foreach (var x in nums1)
{
Console.Write ("{0},", x);
}
Output Result:
1, 2, 4, 3,


Example 2: Querying with a LINQ function

int[] numbers = {5, 1, 8, 6, 2, 6, 4, 9, 10, 7, 3};
var nums1 = numbers. Where (n = n < 5);
foreach (var x in nums1)
{
Console.Write ("{0},", x);
}
Output Result:

1, 2, 4, 3,


Example 3: Using the JOIN clause

Join is similar to join in SQL

Student[] Students =
{
New Student {Id = 1, Name = "Zhangsan"},
New Student {Id = 2, Name = "Lisi"},
New Student {Id = 3, Name = "Wangwu"}
};
studentcourse[] Studentcourses =
{
New Studentcourse {Id = 1, coursename = "Math"},
New Studentcourse {Id = 1, coursename = "Chinese"},
New Studentcourse {Id = 2, Coursename = "Math"},
New Studentcourse {Id = 2, Coursename = "中文版"},
New Studentcourse {Id = 3, Coursename = "Math"}
};

var sc = from S in students
Join C in studentcourses on S.id equals c.id
where c.id = = 1
Select New {s, C};
foreach (var student in SC)
{
Console.WriteLine ("Id={0}, Name={1}, Course={2}", Student.s.id, Student.s.name, student.c.coursename);
}
Output Result:

Id=1, Name=zhangsan, Course=math
Id=1, Name=zhangsan, Course=chinese


Example 4: Using the LET clause

The LET clause takes an operation of an expression and assigns it to an identifier that needs to be used in other operations.

var group1 = new int[] {3, 4, 5, 6};
var group2 = new int[] {6, 7, 8, 9};
Find all the data that satisfies the two number added to 12
var someints = from A in group1
From B in group2
Let sum = a + b
where sum = = 12
Select New {A, B, sum};
foreach (var x in someints)
{
Console.WriteLine (x);
}
Output Result:

{a = 3, b = 9, sum = 12}
{a = 4, b = 8, sum = 12}
{a = 5, b = 7, sum = 12}
{a = 6, b = 6, sum = 12}


Example 5: Using the BY clause

Note that unlike SQL, there are no spaces between order and by, and the default sort is ascending, and you can use the ascending and descending keywords to set the Sort method

int[] numbers = {5, 1, 8, 6, 2, 6, 4, 9, 10, 7, 3};
var results = from N in numbers where n < 5 equals n descending select N;
foreach (var result in results)
{
Console.WriteLine (result);
}
Output Result:

4
3
2
1


Example 6: Using the group...by clause

studentcourse[] Studentcourses =
{
New Studentcourse {Id = 1, coursename = "Math"},
New Studentcourse {Id = 1, coursename = "Chinese"},
New Studentcourse {Id = 2, Coursename = "Math"},
New Studentcourse {Id = 2, Coursename = "中文版"},
New Studentcourse {Id = 3, Coursename = "Math"}
};
var query = from SC in studentcourses
Group SC by SC. Coursename;
foreach (Var sc in query)
{
Console.WriteLine ("Current Group key={0}", SC. Key);//sc. Key is the key value of the current grouping
Note that there is a need to iterate again to get the value of the actual data
foreach (var s in SC)
{
Console.WriteLine ("Id={0}, Coursename={1}", S.id, S.coursename);
}
}
Output Result:

Current Group Key=math
Id=1, Coursename=math
id=2, Coursename=math
Id=3, Coursename=math
Current Group Key=chinese
Id=1, Coursename=chinese
Current Group Key=english
id=2, Coursename=english

Example 7: Query continuation: Using the INTO clause

var group1 = new int[] {3, 4, 5, 6};
var group2 = new int[] {4, 5, 6, 7};
var query = from A in group1
Join B in group2 on a equals b
into group12//query continuation
From C in GROUP12
Select C;
foreach (var item in query)
{
Console.WriteLine (item);
}
Output Result:

4
5
6

Example 8: Standard query operators

The standard query operator consists of a series of APIs that allow us to query any. NET array or collection. The main features of the standard query operators are as follows:

The collection object being queried is called a sequence, and it must implement the Ienumerable<t> interface
Standard query operators use function syntax
Some operators return IEnumerable objects (or other sequences), while others return scalars. Returns a scalar operator that executes immediately and returns a value instead of an enumerable type object.
Many operations take a predicate as a parameter, and a predicate is a method that returns true or false based on the object as a parameter, depending on whether the object satisfies a condition
The following code demonstrates the use of sum and count:

var numbers = new int[] {2, 3, 5};
int total = numbers. Sum ();
int count = numbers. Count ();
Console.WriteLine ("Total={0}, Count={1}", total, Count);
Output Result:

total=10, count=3

Here are all the standard query operators, which you can click to see how to use them:

LINQ Practice Tutorials

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.