0

0

Data Access FAQ (二)

php中文网

php中文网

发布时间:2016-06-07 15:42:59

|

1228人浏览过

|

来源于php中文网

原创

这里是ASP.NET Data Access FAQ的第二部分: LINQ How can I implement a transaction in LINQ? A: You can use TransactionScope class in LINQ to implement a transaction. Its a new function in .NET Framework 2.0 to provide an implicit way to impl

这里是ASP.NET Data Access FAQ的第二部分:
 

linq

How can I implement a transaction in LINQ?

A: You can use TransactionScope class in LINQ to implement a transaction. It’s a new function in .NET Framework 2.0 to provide an implicit way to implement a transaction. You can use it in LINQ as shown below:

using (TransactionScope scope = new TransactionScope())

{

       try

       {

             ……….   

             ctx.SubmitChanges();

             ……….   

             ctx.SubmitChanges();

       }

       catch (Exception ex)

       {

             Response.Write("Error happens, Transaction class will automaticlly roll back!");

       }

 

       scope.Complete();

}

You need to reference the System.Transactions assembly and add the namespace ‘System.Transactions’. Also, you need to make sure the windows service-“Distributed Transaction Coordinator Service” is running.

Related link: 

http://msdn.microsoft.com/en-us/library/system.transactions.transactionscope.aspx

How can I use left join in LINQ.

A: You can use the keywords “join” and “into” to implement left join in LINQ. Please take a look at following example:

var sel = from u in

          join p inon u.TagID equals p.TagID into UP

          from p in UP.DefaultIfEmpty()

          select new

          {

                 UT = u.TagID,

                 UT1 = u.Text,

                 UT2 = p.Info

          };

DaGaoPeng(大高朋网团购程序)
DaGaoPeng(大高朋网团购程序)

大高朋团购系统是一套Groupon模式的开源团购程序,开发的一套网团购程序,系统采用ASP+ACCESS开发的团购程序,安装超简,功能超全面,在保留大高朋团购系统版权的前提下,允许所有用户免费使用。大高朋团购系统内置多种主流在线支付接口,所有网银用户均可无障碍支付;短信发送团购券和实物团购快递发货等。 二、为什么选择大高朋团购程序系统? 1.功能强大、细节完善 除了拥有主流团购网站功能,更特别支

下载

What’s the difference between List and IQueryable?

A: You can return LINQ query result as type of both List and IQueryable. But there are some differences between these two types.

List will create a new list object in memory immediately to persist data. If there’re any associations in this table, the related information will be null. But IQueryable will not retrieve the data until you iterate the data source – use foreach, databind, ToList and so on. When there’s an association in this table, the related information will not be null and can be used. Please take a look at following example to understand the differences between them.

// Return List will fail when referring to related UserInfos object

ListUser> users = res.ToListUser>();

var ss = users.WhereUser>(p => p.UserInfos.ID != 3);

// Return IQueryable will be successful

IQueryableUser> users = res.AsQueryableUser>();

var ss = users.WhereUser>(p => p.UserInfos.ID != 3);

How to implement ‘Like’ operation in LINQ just like in SQL script?

A: If you want to implement the ‘Like’ function in LINQ as in SQL script, you can achieve this by following two methods.

First, you can use Contains, StartsWith, or EndsWith method, here is an example to demonstrate how to use them.

var dd = from p in ctx.Users

         where p.email.Contains("xx") || p.userName.StartsWith("xx") || p.userName.EndsWith("xx")

         select p;

Second, you can use SqlMethods class, it contains a method named ‘Like’ which has the same function with ‘Like’ in SQL script.

var dd = (from p in ctx.Users

         where SqlMethods.Like(p.userName, "%Jiang%") && SqlMethods.Like(p.email,"%WWW%")

         orderby p.accountID

         select p).Take(10);

How to query a DataTable using LINQ?

A: LINQ can query the data source which implements interface IEnumerable. This means you need to first call AsEnumerable method on DataTable, and then you can use LINQ to query the data. Here’s a sample:

var nostr = from u in dt.AsEnumerable()

            where u.FieldDecimal>("m").ToString().ToUpper().StartsWith("3")

            select new

                   {

                   MONEY = u.FieldDecimal>("m"),

                   TIME = u.FieldDateTime>("t"),

                   EXT = "Extra Column"

                   };

相关专题

更多
php源码安装教程大全
php源码安装教程大全

本专题整合了php源码安装教程,阅读专题下面的文章了解更多详细内容。

150

2025.12.31

php网站源码教程大全
php网站源码教程大全

本专题整合了php网站源码相关教程,阅读专题下面的文章了解更多详细内容。

88

2025.12.31

视频文件格式
视频文件格式

本专题整合了视频文件格式相关内容,阅读专题下面的文章了解更多详细内容。

90

2025.12.31

不受国内限制的浏览器大全
不受国内限制的浏览器大全

想找真正自由、无限制的上网体验?本合集精选2025年最开放、隐私强、访问无阻的浏览器App,涵盖Tor、Brave、Via、X浏览器、Mullvad等高自由度工具。支持自定义搜索引擎、广告拦截、隐身模式及全球网站无障碍访问,部分更具备防追踪、去谷歌化、双内核切换等高级功能。无论日常浏览、隐私保护还是突破地域限制,总有一款适合你!

61

2025.12.31

出现404解决方法大全
出现404解决方法大全

本专题整合了404错误解决方法大全,阅读专题下面的文章了解更多详细内容。

493

2025.12.31

html5怎么播放视频
html5怎么播放视频

想让网页流畅播放视频?本合集详解HTML5视频播放核心方法!涵盖<video>标签基础用法、多格式兼容(MP4/WebM/OGV)、自定义播放控件、响应式适配及常见浏览器兼容问题解决方案。无需插件,纯前端实现高清视频嵌入,助你快速打造现代化网页视频体验。

16

2025.12.31

关闭win10系统自动更新教程大全
关闭win10系统自动更新教程大全

本专题整合了关闭win10系统自动更新教程大全,阅读专题下面的文章了解更多详细内容。

12

2025.12.31

阻止电脑自动安装软件教程
阻止电脑自动安装软件教程

本专题整合了阻止电脑自动安装软件教程,阅读专题下面的文章了解更多详细教程。

5

2025.12.31

html5怎么使用
html5怎么使用

想快速上手HTML5开发?本合集为你整理最实用的HTML5使用指南!涵盖HTML5基础语法、主流框架(如Bootstrap、Vue、React)集成方法,以及无需安装、直接在线编辑运行的平台推荐(如CodePen、JSFiddle)。无论你是新手还是进阶开发者,都能轻松掌握HTML5网页制作、响应式布局与交互功能开发,零配置开启高效前端编程之旅!

2

2025.12.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
Pandas 教程
Pandas 教程

共15课时 | 0.9万人学习

SQL 教程
SQL 教程

共61课时 | 3.3万人学习

Kotlin 教程
Kotlin 教程

共23课时 | 2.2万人学习

关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号