惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

B
Blog
D
Docker
J
Java Code Geeks
腾讯CDC
Blog — PlanetScale
Blog — PlanetScale
G
Google Developers Blog
M
MIT News - Artificial intelligence
L
LangChain Blog
T
The Blog of Author Tim Ferriss
P
Proofpoint News Feed
MyScale Blog
MyScale Blog
博客园 - Franky
GbyAI
GbyAI
Hugging Face - Blog
Hugging Face - Blog
aimingoo的专栏
aimingoo的专栏
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
博客园 - 聂微东
N
Netflix TechBlog - Medium
B
Blog RSS Feed
Y
Y Combinator Blog
阮一峰的网络日志
阮一峰的网络日志
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Google DeepMind News
Google DeepMind News

博客园 - 王大湿

2026年抖音、小红书、快手、微信小程序流量玩法差异拆解:适合中小商家的平台选择指南 2026年中小商家打通抖音小红书快手私域流程:微信小程序怎么实现多平台用户统一沉淀变现 改性/生物基尼龙新材料优质公司怎么选?2026年各细分领域头部企业盘点 源于现实的启发性算法:模拟退火与混合策略 AI时代的品牌监测技术发展趋势 append2 给append 添加回调方法 ISO in CSS content 阿里云OSS多图上传 无法将 DROP TABLE 用于 ,因为 是 未知类型。 Entity Framework 多字段排序 阿里巴巴全部行业分类 ServerVariables Levenshtein Distance 字符串的距离 Response.Status 分割DataTable MSN OAuth 2.0 API HTTP 状态代码 正则表达式常用字符、代码 导出扣扣漫游聊天记录 Tencent://Message/协议的实现原理
Entity Framework linq
王大湿 · 2012-06-20 · via 博客园 - 王大湿
 

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)