> 데이터 베이스 > MySQL 튜토리얼 > Data Access FAQ (二)

Data Access FAQ (二)

WBOY
풀어 주다: 2016-06-07 15:42:59
원래의
1007명이 탐색했습니다.

这里是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

          };

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.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿