zoukankan      html  css  js  c++  java
  • 4. 联结操作符—【LINQ标准查询操作符】

    public class Join_LINQ
        {
            static  string ContextString = System.Configuration.ConfigurationSettings.AppSettings["ContextString"].ToString();
            static DataContext context = new DataContext(ContextString);
            static Table<Contact> contact = context.GetTable<Contact>();
            static Table<Employee> emp = context.GetTable<Employee>();
    
            #region Join
            public static void Print_Join()
            {
                var joinQuery = from c in contact
                                join e in emp on c.ContactID equals e.ContactID
                                where c.FirstName.StartsWith("R")
                                orderby c.LastName
                                select new { e.EmployeeID, c.FirstName, c.LastName, c.EmailAddress, e.Title, e.HireDate };
                // 方法语法
                var joinMethodQuery = contact.Join(emp, c => c.ContactID, e => e.ContactID, (c, e) => new { e.EmployeeID, c.FirstName, c.LastName, c.EmailAddress, e.Title, e.HireDate }).Where(c => c.FirstName.StartsWith("R")).OrderBy(c => c.LastName);
    
                foreach (var item in joinQuery.Take(3))
                {
                    Console.WriteLine(item);
                }
    
                Console.ReadKey();
            }
            #endregion
    
            #region GroupJoin
            public static void Print_GroupJoin()
            {
                List<Foods> foods = new List<Foods> { new Foods { Kind = "Meat" }, new Foods { Kind = "Fruit" }, new Foods { Kind = "Drink" } };
                List<Food> food = new List<Food> { 
                            new Food{Name= "Beef",FoodKind="Meat"},
                            new Food{Name = "Chicken",FoodKind = "Meat"},
                            new Food{Name = "Apple",FoodKind = "Fruit"},
                            new Food{Name = "Pear",FoodKind = "Fruit"},
                            new Food{Name = "Coca Cola",FoodKind = "Drink"},
                            new Food{Name = "OX",FoodKind = "Drink"},
                            new Food{Name = "Beer",FoodKind = "Drink"},
                };
    
                var groupJoinQuery = foods.GroupJoin(food, fs => fs.Kind, f => f.FoodKind, (fs, f) => new { FoodKind = fs.Kind, FoodName = f.Select(o => o.Name) });
    
                foreach (var item in groupJoinQuery)
                {
                    Console.WriteLine(item.FoodKind);
                    foreach (var i in item.FoodName)
                    {
                        Console.WriteLine(" " + i.ToString());
                    }                
                }
    
                Console.ReadKey();
            }
            #endregion
        }
    
        public struct Foods
        {
            public string Kind;
        }
    
        public struct Food
        {
            public string Name;
            public string FoodKind;
        }
    
    天天来(http://www.daydaycome.com)】- 精选折扣商品,爆料精选,九块九白菜底价尽在天天来!是一个中立的,致力于帮助广大网友买到更有性价比网购产品的分享平台,每天为网友们提供最受追捧 最具性价比 最大幅降价潮流新品资讯。我们的信息大部分来自于网友爆料,如果您发现了优质的产品或好的价格,不妨给我们爆料(谢绝商家)
  • 相关阅读:
    阿里巴巴的云原生应用开源探索与实践
    Helm 3 发布 | 云原生生态周报 Vol. 27
    带你上手一款下载超 10 万次的 IDEA 插件
    最强CP!阿里云联手支付宝小程序如何助力双11?
    媲美5G的Wifi网速、“备战”资产一键领……揭秘双11小二背后的保障力量
    dubbo-go 的开发、设计与功能介绍
    饿了么交付中心语言栈转型总结
    数据一致性检测的应用场景与最佳实践
    2684亿!阿里CTO张建锋:不是任何一朵云都撑得住双11
    《DNS稳定保障系列3--快如闪电,域名解析秒级生效》
  • 原文地址:https://www.cnblogs.com/Reborn/p/1714423.html
Copyright © 2011-2022 走看看