1. 投影操作符—【LINQ標準查詢操作符】

來源:互聯網
上載者:User
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;    }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.