In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the knowledge of "what is a Linq Lambda expression". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
Linq Lambda expression
Many standard query operators use Func delegates to process individual elements when performing operations on a sequence. Linq Lambda expressions can be used in conjunction with standard query operators to represent delegates. The Linq Lambda expression is an abbreviated expression for creating a delegate implementation and can be used in all situations where anonymous delegates apply. Both C # and Visual Basic ®.NET support Linq Lambda expressions. However, it is important to note that because Visual Basic .NET does not yet support anonymous methods, Linq Lambda expressions may contain only one statement.
Let's look at how to use the Single operator on an array of integers. Each element of this array of integers represents 2 to the power of 1 to 10. Create this array, and then use the Single operator to retrieve a single integer element that meets the criteria specified in the Linq Lambda expression:
Int [] nums = {1,2,4,8,16,32, 64,128,256,512, 1024}; int singleNum = nums.Single (x = > x > 16 & & x)
< 64); Console.WriteLine(singleNum.ToString()); Linq Lambda表达式包含多个关键部分。Linq Lambda表达式首先定义传入委托的变量。在以上代码示例中,x(在 =>Operator) is a parameter that represents each element in the nums array passed to it. The rest of the Linq Lambda expression represents the evaluation logic for each element in the array. You can easily rewrite the above expression using an anonymous delegate, as follows:
Int singleNum = nums.Single (delegate (int x) {return (x > 16 & & x)
< 64); } ) ; 但是,此代码的可读性不及 Linq Lambda表达式。C# 2.0 引入了可使委托的传递稍微轻松些的匿名委托;但是,Linq Lambda表达式的简洁语法可使其更加简单。 First和Single 如果必须从序列中提取一个值,First、FirstOrDefault、Single 和 SingleOrDefault 操作符都非常有用。First 方法返回序列中的***个元素。First 有一个重载方法,可使用它来传入 Linq Lambda表达式来代表一个条件。例如,如果要返回整数序列中整数元素大于 50 的***个元素,可使用以下代码示例: int[] nums = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024 }; int num1 = nums.First(); int num2 = nums.First(x =>X > 50); int num3 = nums.FirstOrDefault (x = > x > 5000); Console.WriteLine (num1.ToString () + "-" + num2.ToString () + "-" + num3.ToString ())
This code looks for * * elements (1), * * elements greater than 50 (64), and * * elements greater than 5000. Because no element in the array satisfies the third Linq Lambda expression (no integer in the array is greater than 5000), an exception is thrown if the code uses the First operator instead of FirstOrDefault. When using the FirstOrDefault operator, 0 is returned if no element satisfies the Linq Lambda expression. The First operator can also be used for LINQ to Entities queries, as follows:
Using (Entities entities = new Entities ()) {var query = (from c in entities.Customers select c) .first (c = > c.City.Equals ("London")); Console.WriteLine (query.CompanyName);}
In this example, you will return * customers in the London city. As you can see, the syntax used does not change when the First method is used for various LINQ providers (in this case, LINQ to Objects and LINQ to Entities).
That's all for "what is an Linq Lambda expression?" Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.