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)
                                  
  • 相关阅读:
    MySQL常用函数大全讲解
    mysql 获取最近一个月每一天
    Mysql查询某个月的每一天的数据
    Mysql 查询一天中,每个小时数据的数量
    oracle 和 mysql 遍历当前月份每一天
    sql查询总结
    Qt样式表——选择器详解(父子关系,插图详细解释)
    Qt样式表之盒子模型(以QSS来讲解,而不是CSS)
    程序员晋升必备技能——单元测试框架(小豆君的干货铺)
    为什么川普反对中国补贴农业(渐进式发展是非常正确的,如果贸然改动农村土地制度,在城市还不能提供足够的就业岗位下将大量的农民推向城市……请欣赏一下巴西、印度城市的贫民窟)
  • 原文地址:https://www.cnblogs.com/hun_dan/p/2556530.html
Copyright © 2011-2022 走看看