然后是我自己写的关于分页查询(初学linq,不知道是不是规范的):public static class DataContextExtensions
{
public static List<T> ExecuteQuery<T>(this DataContext dataContext, IQueryable query)
{
DbCommand command = dataContext.GetCommand(query);
dataContext.OpenConnection();
using (DbDataReader reader = command.ExecuteReader())
{
return dataContext.Translate<T>(reader).ToList();
}
}
private static void OpenConnection(this DataContext dataContext)
{
if (dataContext.Connection.State == ConnectionState.Closed)
{
dataContext.Connection.Open();
}
}
}
private static string con = ConfigurationManager.AppSettings["ConnectionString"];
private DataClassDataContext dc = new DataClassDataContext(con);
public List<Customers> QueryCustomer(string customer,int PageSize,int PageIndex)
{
Table<Customers> TBcustomer = dc.GetTable<Customers>();
var query = from a in TBcustomer
where a.Customer.Contains(customer)
select
new
{
ID = a.ID,
Customer = a.Customer,
Name = a.Name,
EmployeeID = a.EmployeeID,
EmployeeName = a.EmployeeName,
Region = a.Region,
Country = a.Country,
BU = a.BU,
Tiers = a.Tiers,
NewGroup = a.NewGroup
};
using (dc.Connection)
{//上面的类中的扩展方法如果没有,此句会报错
return dc.ExecuteQuery<Customers>(query.Skip((PageIndex - 1) * PageSize).Take(PageSize));
}