• 1. 投影操作符—【LINQ标准查询操作符】


    public class Select_LINQ
        {
            public static  string ContextString = System.Configuration.ConfigurationSettings.AppSettings["ContextString"].ToString();
            public static void Print()
            {
                DataContext context = new DataContext(ContextString);
                Table<Contact> contact = context.GetTable<Contact>();
    
                var querySyntex = from c in contact
                            where c.FirstName.StartsWith("S")
                            select new { c.FirstName, c.LastName, c.EmailAddress };
    
                var methodSyntex = contact.Select(c => new { c.FirstName, c.LastName, c.EmailAddress }).Where(con => con.FirstName.StartsWith("S"));
    
                Console.WriteLine("============== Query Syntex ==============");
                foreach (var item in querySyntex.Take(10))
                {                
                    Console.WriteLine(item.ToString());
                }
    
                Console.WriteLine("\n");
    
                Console.WriteLine("============== Method Syntex ==============");
                foreach (var item in methodSyntex.Take(10))
                {                
                    Console.WriteLine(item.ToString());
                }
    
                Console.ReadKey();
            }
        }
    
        public class SelectMany_LINQ
        {
            public static void Print()
            {
                Names[] names = { 
                  new Names{FirstName = "J. Phillip",Pets = new List<string>{"Yudon","Fido"}},
                  new Names{FirstName = "Michelle",Pets = new List<string>{"Alexander","Alcorn"}},
                  new Names{FirstName = "Maxwell",Pets = new List<string>{"Alpuerto","Arakawa"}} };
                IEnumerable<string> nameQuery = names.AsQueryable().SelectMany(n => n.Pets);            
                //List<string> list = nameQuery.ToList<string>();// 立即执行
    
                foreach (var item in nameQuery)
                {
                    Console.WriteLine(item);
                }
    
                Console.WriteLine("\n");
                // 注意nameQuery 与 nameQuery2 的区别
                /*
                 * IEnumerable接口提供了向前遍历的功能,它不具有在各个数据项之间移动(向前移动除外)的能力
                 * 然而,IQueryable可以可以使查询操作更具灵活性,虽然是IQueryable实现了IEnumerable,但IEnumerable
                 * 为IQueryable提供了遍历功能。
                 */
                IQueryable nameQuery2 = names.AsQueryable().Select(n => n);
                foreach (var item in nameQuery2)
                {
                    Console.WriteLine(((Names)item).FirstName);
                    foreach (var p in ((Names)item).Pets)
                    {
                        Console.WriteLine(" " + p.ToString());
                    }                
                }
    
                Console.ReadKey();
            }
        }
    
        struct Names
        {
          public string FirstName;       
          public List<string> Pets;
        }
    
  • 相关阅读:
    浅入ABP(1):搭建基础结构的 ABP 解决方案
    浅入ABP(2):添加基础集成服务
    GDB 调试 .NET 程序实录
    浅入 ABP 系列(6):数据库配置
    浅入 ABP 系列(4):事件总线
    浅入 ABP系列(3):增加日志组件、依赖注入服务
    模拟IIC总线多设备挂载(12864OLED屏幕+GY30光照传感器)
    RabbitMQ与Kafka选型对比
    .Net在Windows上使用Jenkins做CI/CD的那些事
    Tomcat乱码问题
  • 原文地址:https://www.cnblogs.com/Reborn/p/1714415.html
Copyright © 2020-2023  润新知