以下是我的LINQ
我想重新写一下LINQ
,好像flag
是true
,然后获取所有其他记录,然后申请.skip()
&这里提到的take()
public IEnumerable<ReportMapper> FetchReports(DateTime startDate, DateTime endDate, int docMode, int pageNumber, bool flag )
{
try
{
IEnumerable<ReportMapper> reports;
using (var entities = new DatabaseEntities1())
{
IQueryable<ReportMapper> query;
if (docMode > 0)
{
query = (from c in entities.tDocumentStatus
join d in entities.tTOCStructures on c.DocumentId equals d.DocumentID
join e in entities.tUsers on d.LastUpdatedBy equals e.UserUID
orderby c.AssignedDate descending
where c.AssignedDate >= startDate && c.AssignedDate <= endDate && c.StatusId == docMode
select new ReportMapper()
{
DocumentName = d.FolderName,
AssignedDate = c.AssignedDate,
ReviewStatus = c.tStatu.StatusName,
ActionPerformedBy = e.FirstName + " " + e.LastName
}).Skip(pageNumber * 10).Take(50);
}
else
{
query = (from c in entities.tDocumentStatus
join d in entities.tTOCStructures on c.DocumentId equals d.DocumentID
join e in entities.tUsers on d.LastUpdatedBy equals e.UserUID
orderby c.AssignedDate descending
where c.AssignedDate >= startDate && c.AssignedDate <= endDate
select new ReportMapper()
{
DocumentName = d.FolderName,
AssignedDate = c.AssignedDate,
ReviewStatus = c.tStatu.StatusName,
ActionPerformedBy = e.FirstName + " " + e.LastName
}).Skip(pageNumber * 10).Take(50);
}
reports = query.ToList<ReportMapper>();
return reports;
}
}
catch (Exception ex)
{
//handle exception
}
}
但不知道我将如何做到这一点。
注意: -
请注意,上面查询中的var docMode
用于检查是否在where子句中应用第三个条件(检查我的where子句是否全部不同)
如果不使用if-else阶梯,有没有更好的方法。
答案 0 :(得分:2)
您可以简化对此的查询:
public IEnumerable<ReportMapper> FetchReports(DateTime startDate, DateTime endDate, int docMode, int pageNumber, bool flag)
{
using (var entities = new DatabaseEntities1())
{
IQueryable<ReportMapper> reports =
from c in entities.tDocumentStatus
join d in entities.tTOCStructures on c.DocumentId equals d.DocumentID
join e in entities.tUsers on d.LastUpdatedBy equals e.UserUID
orderby c.AssignedDate descending
where c.AssignedDate >= startDate && c.AssignedDate <= endDate
&& (docMode <= 0 || c.StatusId == docMode)
select new ReportMapper()
{
DocumentName = d.FolderName,
AssignedDate = c.AssignedDate,
ReviewStatus = c.tStatu.StatusName,
ActionPerformedBy = e.FirstName + " " + e.LastName
};
if(!flag)
reports = reports.Skip(pageNumber * 10).Take(50);
return reports.ToList();
}
}
答案 1 :(得分:1)
我这样做:
public IEnumerable<ReportMapper> FetchReports(DateTime startDate, DateTime endDate, int docMode, int pageNumber, bool flag) {
try {
IEnumerable<ReportMapper> reports;
using (var entities = new DatabaseEntities1()) {
var query = (from c in entities.tDocumentStatus
join d in entities.tTOCStructures on c.DocumentId equals d.DocumentID
join e in entities.tUsers on d.LastUpdatedBy equals e.UserUID
orderby c.AssignedDate descending
where c.AssignedDate >= startDate && c.AssignedDate <= endDate && (docMode <= 0 || c.StatusId == docMode)
select new ReportMapper() {
DocumentName = d.FolderName,
AssignedDate = c.AssignedDate,
ReviewStatus = c.tStatu.StatusName,
ActionPerformedBy = e.FirstName + " " + e.LastName
});
if (!flag)
query = query.Skip(pageNumber*10).Take(50);
reports = query.ToList<ReportMapper>();
return reports;
}
}
catch (Exception ex) {
//handle exception
}
}
答案 2 :(得分:0)
在您的查询后添加:
if (!flag) query = query.Skip(pageNumber * 10).Take(50);
答案 3 :(得分:0)
query = (from c in entities.tDocumentStatus
join d in entities.tTOCStructures on c.DocumentId equals d.DocumentID
join e in entities.tUsers on d.LastUpdatedBy equals e.UserUID
orderby c.AssignedDate descending
where c.AssignedDate >= startDate && c.AssignedDate <= endDate && c.StatusId == docMode
select new ReportMapper()
{
DocumentName = d.FolderName,
AssignedDate = c.AssignedDate,
ReviewStatus = c.tStatu.StatusName,
ActionPerformedBy = e.FirstName + " " + e.LastName
});
if(!flag){
query = query.Skip(pageNumber * 10).Take(50);
}
答案 4 :(得分:0)
{{1}}