Kgn-web Kgn-web - 8 days ago 5
C# Question

How to apply if condition in LINQ?

Below is my

LINQ


I want to re-write my
LINQ
as if
flag
is
true
then fetch all the records else apply
.skip()
&
take()
as mentioned here

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
}
}


But no clue how will I do this.

Note:-
Please note that the var
docMode
in the above query is used for checking whether to apply the third condition in where clause or not (Check my where clause that's all together different)

Is there a better way to do without using if-else ladder.

Answer

You could simplify your queries to this:

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();
    }
}