zoukankan      html  css  js  c++  java
  • Entity Framework linq

     static void Main(string[] args)
            {
                using (var oAWEntities = new AdventureWorksEntities())
                {
                    // 1. Easy example but not very flexible
                    
    //    Select all products without any constraints
                    foreach (var product in oAWEntities.Product)
                    {
                        Console.WriteLine(string.Format("{0} | {1} | {2}",
                            product.ProductID, product.Name, product.ListPrice));
                    }
                    Console.ReadLine();

                    // 2. Same example as above using LINQ
                    foreach (var product in from p in oAWEntities.Product
                                            select p)
                    {
                        Console.WriteLine(string.Format("{0} | {1} | {2}",
                            product.ProductID, product.Name, product.ListPrice));
                    }
                    Console.ReadLine();

                    // 3. Same example as above externalizing the LINQ query
                    
    //    Select 20 products without any constraints
                    var query = from p in oAWEntities.Product
                                select p;

                    foreach (var product in query.Take(20))
                    {
                        Console.WriteLine(string.Format("{0} | {1} | {2}",
                            product.ProductID, product.Name, product.ListPrice));
                    }
                    Console.ReadLine();

                    // 4. Example using Lambda Expressions
                    
    //    Select 20 products that have a review rating above 3
                    var queryLambda = from p in oAWEntities.Product
                                where p.ProductReview.Any(pr => pr.Rating > 3)
                                select p;

                    foreach (var product in queryLambda.Take(20))
                    {
                        Console.WriteLine(string.Format("{0} | {1} | {2}",
                            product.ProductID, product.Name, product.ListPrice));
                    }

                    Console.ReadLine();
                    Console.Clear();

                    // 5. This serves to see what SQL queries are send to the Database
                    var oQuery = (System.Data.Objects.ObjectQuery<Product>)query;
                    Console.WriteLine(oQuery.ToTraceString());
                    Console.ReadLine();

                    using (var oAWEntities2 = new AdventureWorksEntities())
                    {
                        // 6. Select the count of all products that are sold out
                        
    //    This will only work if you re-add the deleted MakeFlag
                        
    //var querySoldOut = from p in oAWEntities2.Product
                        
    //                   where p.MakeFlag == true
                        
    //                   select p;
                        
    //Console.WriteLine(string.Format("SoldOutProductCount: {0}", querySoldOut.Count()));
                        
    //Console.ReadLine();

                        
    // 7. Select the count of all products that are sold out
                        var querySoldOut = from p in oAWEntities2.Product
                                           where p is SoldOutProduct
                                           select p;

                        Console.WriteLine(string.Format("SoldOutProductCount: {0}", querySoldOut.Count()));
                        Console.ReadLine();
                    }

                    // 8. Update the StandardCost value
                    using (var oAWEntities4 = new AdventureWorksEntities())
                    {
                        var queryForUpdateCost = from p in oAWEntities4.Product
                                    where p.ProductID.Equals(3)
                                    select p;

                        var product = queryForUpdateCost.First();

                        Console.WriteLine(string.Format("{0} | {1} | {2}",
                            product.Name, product.ProductNumber, product.StandardCost));
                        Console.ReadLine();

                        product.StandardCost += 100;
                        oAWEntities4.SaveChanges();
                    }

                    // 9. Query the StandardCost value to verify update
                    using (var oAWEntities5 = new AdventureWorksEntities())
                    {
                        var queryForUpdateCost2 = from p in oAWEntities5.Product
                                    where p.ProductID.Equals(3)
                                    select p;

                        var product = queryForUpdateCost2.First();

                        Console.WriteLine(string.Format("{0} | {1} | {2}",
                            product.Name, product.ProductNumber, product.StandardCost));
                        Console.ReadLine();
                    }

                    // 10. Try to set the StandardCost value to a negative value --> get exception
                    using (var oAWEntities6 = new AdventureWorksEntities())
                    {
                        var queryForUpdateCost3 = from p in oAWEntities6.Product
                                    where p.ProductID.Equals(3)
                                  
  • 相关阅读:
    (转)Unity3D 开发优秀技术资源汇总
    (转)Unity3d通过Action注册事件,回调方法
    (转)Unity3D研究院之游戏架构脚本该如何来写(三十九)
    (转)Unity3D研究院之异步加载游戏场景与异步加载游戏资源进度条(三十一)
    Unity3D的主要类图
    C# 事件和Unity3D
    unity3d 场景配置文件生成代码
    497. Random Point in Non-overlapping Rectangles
    478. Generate Random Point in a Circle
    470. Implement Rand10() Using Rand7() (拒绝采样Reject Sampling)
  • 原文地址:https://www.cnblogs.com/hun_dan/p/2556530.html
Copyright © 2011-2022 走看看