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

本文通过示例展示了如何使用C#中的LINQ进行数据查询,包括使用查询语法和方法语法两种方式,并介绍了SelectMany扩展方法的应用场景。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }

转载于:https://www.cnblogs.com/Reborn/archive/2010/04/17/1714415.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值