首页 > 代码库 > Linq to EF 和 Linq to SQL 中间Linq 产生的SQL语句

Linq to EF 和 Linq to SQL 中间Linq 产生的SQL语句

一,Linq to EF

using System;
using System.Runtime.CompilerServices;
using System.Linq;
using System.Data;
 
namespace EntityframeworkSQL
{
    class Program
    {
        static void Main(string[] args)
        {
 
            using (CustomerEntities customerEntities = new CustomerEntities())
            {
                var customerNames = from c in customerEntities.Customers
                             select c.CustomerName;
                string sql = ((System.Data.Objects.ObjectQuery)customerNames).ToTraceString();
 
                Console.WriteLine(sql);
 
            }
             
        }
    }
}

 

二,Linq to sql ;

using System;
using System.Linq;
using System.Data;
 
namespace LinqToSQL
{
    class Program
    {
        static void Main(string[] args)
        {
            using (CustomerDataContext customerContext = new CustomerDataContext())
            {
                var customerNames = from c in customerContext.Customers
                                    select c.CustomerName;
                string sql = customerContext.GetCommand(customerNames).CommandText;
 
                Console.WriteLine(sql);
            }
        }
    }
}

 

最近学习的linq to EF 找到的!