Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How Linq uses Select

2025-02-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article is about how Linq uses Select. The editor thinks it is very practical, so share it with you as a reference and follow the editor to have a look.

First of all, let you understand the Linq To Sql query database, and then a comprehensive introduction to Linq using Select.

Here are some examples to illustrate how to use Select in Linq, refer to: LINQ Samples

1. You can make some transformations to the query results. The following example looks for names that start with "B" in the array and converts them all to lowercase output:

String [] names = {"Jack", "Bob", "Bill", "Catty", "Willam"}; var rs = from n in names where n.StartsWith ("B") select n.ToLower (); foreach (var rin rs) Console.WriteLine (r)

two。 Return anonymous types. For example, when Linq To Sql queries a database, only the required information is returned. The following example is querying the Customer table in the Northwind database and returning the ID and names of all customers whose names begin with "B":

NorthwindDataContext dc = new NorthwindDataContext (); var cs = from c in dc.Customers where c.ContactName.StartsWith ("B") select new {CustomerID = c.CustomerID, CustomerName = c.ContactTitle + "" + c.ContactName}; foreach (var c in cs) Console.WriteLine (c)

3. For arrays, select can manipulate array elements as well as indexes:

String [] names = {"Jack", "Bob", "Bill", "Catty", "Willam"}; var rs = names.Select ((name, index) = > new {Name = name, Index = index}); foreach (var rin rs) Console.WriteLine (r)

4. Combined query, you can combine conditional queries against multiple data sources (equivalent to Linq using the SelectMany function). The following example is actually relative to a double loop traversal:

Int [] numbersA = {0,2,4,5,6,8,9}; int [] numbersB = {1,3,5,7,8}; var pairs = from an in numbersA, b in numbersB where a < b select new {a, b}; Console.WriteLine ("Pairs where a < b:"); foreach (var pair in pairs) Console.WriteLine ("{0} is less than {1}", pair.a, pair.b)

With Linq To Sql, it is equivalent to doing a subquery:

NorthwindDataContext dc = new NorthwindDataContext (); var rs = from c in dc.Customers from o in c.Orders where o.ShipCity.StartsWith ("B") select new {CustomerName = c.ContactName, OrderID = o.OrderID}; foreach (var r in rs) Console.WriteLine (r); Thank you for reading! This is the end of this article on "how Linq uses Select". I hope the above content can be of some help to you, so that you can learn more knowledge. if you think the article is good, you can share it out for more people to see!

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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report