1)分页
public IQueryable<Customer> GetCustomer(out int count, int pageSize, int pageIndex)
{ count = db.Count(); //动态生成查询表达式
var query = db.Customer.OrderBy(customer=> customer.CustomerID) .Skip(pageSize * (pageIndex - 1)).Take(pageSize); //返回查询的表达式目录树 return query; }
2)list及Iquery之间的转换
IQueryable<TrainClassModels> query;
ELearningLinqDataContext db = new ELearningLinqDataContext();
IQueryable<TrainClassModels> p = from c in db.Train_Class
join d in db.Sys_DepartMent on c.Department equals (int)d.DepartMentID
join bi in db.Basic_BaseInfo on c.TrainTargetID equals Convert.ToInt32(bi.BaseInfoCode)
join bty in db.Basic_TrainType on c.TrainTypeID equals bty.TrainTypeID
join e in db.Train_PlanItem on c.PlanItemID equals e.PlanItemID into enull
from newe in enull.DefaultIfEmpty()
where bi.BaseInfoType == PublicClass.traintype && d.DepartMentCode.Contains(PublicClass.getDepartEfficientCode(DepartMentCode))
&& c.ClassName.Contains(classname)
select new TrainClassModels
{
Flag = (int)c.Flag,
Department = (int)c.Department,
AddTime = (DateTime)c.AddTime,
BeginTime = c.BeginTime,
ClassID = c.ClassID,
AddUser = c.AddUser,
ClassName = c.ClassName,
ClassPlace = c.ClassPlace,
DepartmentName = d.DepartMentName,
EndTime = c.EndTime,
IsForce = c.IsForce,
IsReport = c.IsReport,
PlanItemID = c.PlanItemID,
ReportContent = c.ReportContent,
TrainCate = c.TrainCate,
INOutTrain = c.INOutTrain,
NeedCourseWare = (bool)c.NeedCourseWare,
NeedExam = (bool)c.NeedExam,
OutsideTrainInstitution = c.OutsideTrainInstitution,
Remarks = c.Remarks,
ResponsibleMan = c.ResponsibleMan,
TrainTypeID = c.TrainTypeID,
TrainHours = c.TrainHours,
TrainInstitutionDepart = c.TrainInstitutionDepart,
TrainPersonTimes = c.TrainPersonTimes,
TrainTargetID = c.TrainTargetID,
TrainUserCount = c.TrainUserCount,
// TrainTeacher = c.tr,
TrainTargetTitle = bi.BaseInfoName,
TrainTypeName = bty.TrainTypeName,
trainContent = c.trainContent,
PlanID = newe.PlanID ?? -1,
};
query = p;
IQueryable<EmployeeModels> query;
List<EmployeeModels> list;
if (pageIndex >= 0)
list = query.OrderByDescending(a => a.EntrolDate).Skip(pageIndex * pageSize).Take(pageSize).ToList();
else
list = query.OrderByDescending(a => a.EntrolDate).ToList();
return list;