user13829188
user13829188

Reputation:

How to handle date range query in c#

I have a params which contains a start date and end date and then use it to query, but I wanted to handle that even if there is no start and enddate, it will will query data. How do we handle that in C#?

So that if there is no startDate and endDate, then it will just proceed on the query.

The filteredData variable is the query. The issue right now is that when there is no startDate and endDate it will not query the data, so the solution if to handle date range if it has no value. Any idea guys? Thanks.

#code snippet

public async Task<IPagedList<TransactionListDto>> GetList(DateTime? startDate , DateTime? endDatestring status, int id, string sortKey, string sortOrder, string searchString, int page, int pageSize, string transactionType, string repmFilter, string transactionSubType, string masterBrokerCompany, string masterBrokerName)
{
    var sortKeys = JsonConvert.DeserializeObject<List<string>>(sortKey);
    var sortOrders = JsonConvert.DeserializeObject<List<string>>(sortOrder);
    List<string> statusValues = new List<string>();
    List<string> transactionTypeValues = new List<string>();

    if (!string.IsNullOrEmpty(status))
    {
        statusValues = status.Split(',').ToList();
    }
    if (!string.IsNullOrEmpty(transactionType))
    {
        transactionTypeValues = transactionType.Split(',').ToList();
    }
    

    .......


    var filteredData = mappedData.Where(x => (masterBrokerCompanyValues.Count == 0 || masterBrokerCompanyValues.Contains(x.MasterBrokerCompany)) && x.TargetCompletionDate >= startDate && endDate <= x.TargetCompletionDate);
    var paginatedData = await AppUtil.MultipleSort<TransactionListDto>(
    filteredData.AsQueryable(), sortKeys, sortOrders).ToPagedListAsync(page, pageSize);

Upvotes: 0

Views: 864

Answers (3)

Radek Sedl&#225;ř
Radek Sedl&#225;ř

Reputation: 120

If I understand your problem correctly, if startDate and endDate are not given, your query returns an empty list.

It's probably because when you create instance of Datetime, it is automatically initialized to default value. That means, that both values are same. Try to find lowest possible date and highest possible date and set them when initialized.

Datetime startDate = DateTime.MinValue;
Datetime endDate= DateTime.MaxValue;

Upvotes: 0

maraaaaaaaa
maraaaaaaaa

Reputation: 8163

The short answer for what you want is to make those optional parameters:

public async Task<IPagedList<TransactionListDto>> GetList(string status, int id, string sortKey, string sortOrder, string searchString, int page, int pageSize, string transactionType, string repmFilter, string transactionSubType, string masterBrokerCompany, string masterBrokerName, DateTime? startDate = null, DateTime? endDate = null)

Note: they have to be moved to the end.

The better answer is to consolidate all your parameters into a Filter class, as it appears you are using them as filters. This will allow more of them to become optional as well.

public class Filter {
    public string status { get; set; }
    public int id { get; set; }
    public string sortKey { get; set; }
    public string sortOrder { get; set; }
    public string searchString { get; set; }
    public int page { get; set; }
    public int pageSize { get; set; }
    public string transactionType { get; set; }
    public string repmFilter { get; set; }
    public string transactionSubType { get; set; }
    public string masterBrokerCompany { get; set; }
    public string masterBrokerName { get; set; }
    public DateTime? startDate { get; set; }
    public DateTime? endDate { get; set; }
}

public async Task<IPagedList<TransactionListDto>> GetList(Filter filter) {
    ...
} 

The great thing about the Filter class is that if it's coming from your action method in MVC for example, your signature can change in the exact same way and it will just work.

Upvotes: 1

Seb
Seb

Reputation: 1230

Just replace your potential null values by something that won't be limiting :

Datetime startDateForFilter = startDate ?? Datetime.Min;
Datetime endDateForFilter = endDate ?? Datetime.Max;

Upvotes: 1

Related Questions