您的位置:首页 > 编程学习 > ASP.NET > 正文

linq中let

更多 时间:2014-9-19 类别:编程学习 浏览量:1057

linq中let

linq中let

linq中let关键字就是对子查询的一个别名,let子句用于在查询中添加一个新的局部变量,使其在后面的查询中可见。

 

linq中let关键字实例

 

1、传统下的子查询与LET关键字的区别

  •  
  •  
  • C# 代码   复制
  • 
    static void Main(string[] args)
    {
        int[] numbers = new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; 
    
        //传统下的子查询做法
        var query = from num in numbers
                    select num * (from n in numbers
                                  where n % 2 == 0
                                  select n).Count(); 
    
        //使用LET关键字的做法
        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、把每个单词开头包含a或者e的找出来

  •  
  •  
  • C# 代码   复制
  • 
    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(' ')//用空格分割成数组                
                        from word in words
                        let w = word.ToLower()//把每个字母小写        
                        where w[0] == 'a' || w[0] == 'e'
                        select word;
    
            foreach (var s in query)
            {
                Console.WriteLine(s);
            }
            Console.ReadLine();
        }
    }
    
    		
  •  

    3、linq实例3

  •  
  •  
  • C# 代码   复制
  • 
    var query = from p in persons
                let friendlyName = p.Gender == "" ? "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实例4

  •  
  •  
  • C# 代码   复制
  • 
    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 = a.Name
                            let c=a.Age
                            where b =="zhangs" & c>21
                            select a;
                foreach (var item in query)
                {
                    Response.Write("姓名: "+item.Name+"  年龄:"+item.Age);
                }
    
    //结果           姓名: zhangs 年龄:25
    //使用let 建立了个范围变量,这个范围变量在后续的where子句中使用,如果不使用let子句,where子句的表达式将写成这样:
    //where a.Name=="zhangs" & a.Age>21</span>
    
    			
  •  

    标签:linq