Let keyword learning and linqlet keyword learning in linq

Source: Internet
Author: User

Let keyword learning and linqlet keyword learning in linq

In linq, The let keyword is an alias for subqueries. The let clause is used to add a new local variable to the query to make it visible in subsequent queries.

 

Examples of let keywords in linq

 

1. Differences between traditional subqueries and LET keywords

C # code Replication
Static void Main (string [] args) {int [] numbers = new [] {1, 2, 3, 4, 5, 6, 7, 8, 9 }; // The traditional subquery method var query = from num in numbers select num * (from n in numbers where n % 2 = 0 select n ). count (); // using the LET keyword var query = from num in numbers let evenNumbers = from n in numbers where n % 2 = 0 select n select num * evenNumbers. count (); foreach (var item in query) {Console. writeLine (item);} Console. read ();}

 

2. Find out the characters whose names start with a or e.

C # code Replication
Using System; using System. linq; public class Test {static void Main (string [] args) {string [] strings = {"A penny saved is a penny earned. "," The aaxly sdj "," the pa is no "}; var query = from sentence in strings let words = sentence. split ('') // use spaces to Split the string into arrays from word in words let w = word. toLower () // lowercase where w [0] = 'A' | w [0] = 'E' select word; foreach (var s in query) {Console. writeLine (s);} Console. readLine ();}}

 

3. linq instance 3

C # code Replication
Var query = from p in persons let friendlyName = p. Gender = "male "? "Mr": "Ms" + p. name select new {UserID = p. ID, FriendName = friendlyName}; foreach (var item in query) {Console. writeLine ("No: {0}, Friendly Name: {1}", item. userID, item. friendName );}

 

4. linq instance 4

C # code Replication
Public class Singer {public string Name {set; get;} public int Age {set; get ;}list <Singer> List = new list <Singer> () {new Singer {Name = "zhangs", Age = 21}, new Singer {Name = "zhangs", Age = 25}, new Singer {Name = "margy ", age = 21 }}; var query = from a in list let B =. name let c =. age where B = "zhangs" & c> 21 select a; foreach (var item in query) {Response. write ("name:" + item. name + "Age:" + item. age);} // result name: zhangs Age: 25 // use let to create a range variable. This range variable is used in subsequent where clauses. If the let clause is not used, the expression of the where clause will be written as follows: // where. name = "zhangs" &. age> 21 </span>

In C #, how can I use Linq to retrieve multiple keywords in multiple columns in SQL Server tables?

Var result = from p in Wuzi
Where keyword. Contains (p. Column1)
| Keyword. Contains (p. Column2)
| Keyword. Contains (p. Column3)
Selelct p;

Well, this is probably the case.
 
What are the tutorials for beginners of LINQ?

Of course it's MSDN.
Msdn.microsoft.com/zh-cn/library/bb1_676.aspx
Reference: msdn.microsoft.com/zh-cn/library/bb1_676.aspx

Related Article

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.