Linq select 语法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Linq select 语法相关的知识,希望对你有一定的参考价值。

文档:https://code.msdn.microsoft.com/101-LINQ-Samples-3fb9811b

1.可以对查询出来的结果做一些转换,下面的例子在数组中查找以"B"开头的名字,然后全部转成小写输出

string[] names = { "Jack", "Bob", "Bill", "Catty", "Willam" };
            var rs = from n in names
                     where n.StartsWith("B")
                     select n.ToLower();
            foreach (var item in rs)
            {
                Response.Write(item+"<br />");
            }

2.返回匿名类型,只返回需要的信息

var users = from c in UserList
                        where c.Mobile.StartsWith("135")
                        select new
                               {
                                   Name = c.Name,
                                   Contact = c.Address + " " + c.Mobile
                               };

3.使用原数组,产生两组新数组

public void Linq9() 
{ 
    string[] words = { "aPPLE", "BlUeBeRrY", "cHeRry" }; 
  
    var upperLowerWords = 
        from w in words 
        select new { Upper = w.ToUpper(), Lower = w.ToLower() }; 
  
    foreach (var ul in upperLowerWords) 
    { 
        Console.WriteLine("Uppercase: {0}, Lowercase: {1}", ul.Upper, ul.Lower); 
    } 
}

4.判断数字是否与他的下标一致

int[] numbers = { 5, 4, 1, 3, 9, 8, 6, 7, 2, 0 };

            var numsInPlace = numbers.Select((num, index) => new { Num = num, InPlace = (num == index) });

            Console.WriteLine("Number: In-place?");
            foreach (var n in numsInPlace)
            {
                Response.Write(string.Format("{0}: {1}<br />", n.Num, n.InPlace));
            }

5.获得A小于B的所有可能的集合,返回 拿A中的每个元素和B中的每个元素对比的结果

int[] numbersA = { 0, 2, 4, 5, 6, 8, 9 }; 
    int[] numbersB = { 1, 3, 5, 7, 8 }; 
  
    var pairs = 
        from a in numbersA 
        from 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); 
    } 

6.从一个集合的子集中过滤,金额小于500的订单

List<Customer> customers = GetCustomerList();

            var orders =
                from c in customers
                from o in c.Orders
                where o.Total < 500.00
                select new { c.CustomerID, o.OrderID, o.Total };

7.查询所有大于指定日期的用户订单

List<Customer> customers = GetCustomerList(); 
var orders = from c in customers from o in c.Orders where o.OrderDate >= new DateTime(1998, 1, 1) select new { c.CustomerID, o.OrderID, o.OrderDate };

延伸:
List<Customer> customers = GetCustomerList(); 
  
    DateTime cutoffDate = new DateTime(1997, 1, 1); 
  
    var orders = 
        from c in customers 
        where c.Region == "WA" 
        from o in c.Orders 
        where o.OrderDate >= cutoffDate 
        select new { c.CustomerID, o.OrderID }; 

8.代替二重for循环

参考:http://www.cnblogs.com/manupstairs/archive/2012/11/27/2790114.html

http://www.cnblogs.com/lyout/archive/2012/11/28/2792622.html

List<Customer> customers = GetCustomerList(); 
  
    var customerOrders = 
        customers.SelectMany( 
            (cust, custIndex) => 
            cust.Orders.Select(o => "Customer #" + (custIndex + 1) + " has an order with OrderID " + o.OrderID));

  

 

 

以上是关于Linq select 语法的主要内容,如果未能解决你的问题,请参考以下文章

LINQ的实例

Linq 语法

SQL-LINQ-Lambda语法对照

linq语法

Linq语法详细

Linq语法详细