0

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);
3
  • You really need to consolidate all your function parameters into a Filter class and change the signature to GetList(Filter filter). That's wayy to many parameters imo. Commented Feb 2, 2022 at 17:32
  • Noted on this one Sir, but you have any idea with my question ? Commented Feb 2, 2022 at 17:33
  • If I underestant your problem correctly. Your problem is that, if startDate and endDate are not given, your querry returns empty list. Its 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 inicialized. Commented Feb 2, 2022 at 17:36

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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

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

1 Comment

Hi, is this to be added on the GetList params ? or should I declare that below?/
0

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;

2 Comments

Hi, is this to be added on the GetList params ? or should I declare that below?/

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.