Currently I have a method to create a filter which filters by name, address and age using regex in my c# backend. I want to remove the if conditions and use a loop instead. How can I do that?
This is my current method.
private static FilterMethod<Logs> GetFilter(Parameters parameters, DateTime startDate, DateTime endDate)
{
var filter = Builders<Logs>.Filter.Gte(x => x.Date, startDate);
filter &= Builders<Logs>.Filter.Lte(x => x.Date, endDate);
if (!string.IsNullOrEmpty(parameters.Name))
{
filter &= Builders<Logs>.Filter.Regex(
"Name",
new BsonRegularExpression(parameters.Name, "mi"));
}
if (!string.IsNullOrEmpty(parameters.Address))
{
filter &= Builders<Logs>.Filter.Regex(
"Address",
new BsonRegularExpression(parameters.Address, "mi"));
}
if (!string.IsNullOrEmpty(parameters.Age))
{
filter &= Builders<Logs>.Filter.Regex(
"Age",
new BsonRegularExpression(parameters.Age, "mi"));
}
return filter;
}
This is my model class
public class Parameters
{
public string Name { get; set; }
public string Address { get; set; }
public string Age { get; set; }
}
Here startDate and endDate are not null. Always I am passing the current date from frontend.
I have to use dictionary to get the input parameters. Can someone help?
Thank you!!
Currently I have a method to create a filter which filters by name, address and age using regex in my c# backend. I want to remove the if conditions and use a loop instead. How can I do that?
This is my current method.
private static FilterMethod<Logs> GetFilter(Parameters parameters, DateTime startDate, DateTime endDate)
{
var filter = Builders<Logs>.Filter.Gte(x => x.Date, startDate);
filter &= Builders<Logs>.Filter.Lte(x => x.Date, endDate);
if (!string.IsNullOrEmpty(parameters.Name))
{
filter &= Builders<Logs>.Filter.Regex(
"Name",
new BsonRegularExpression(parameters.Name, "mi"));
}
if (!string.IsNullOrEmpty(parameters.Address))
{
filter &= Builders<Logs>.Filter.Regex(
"Address",
new BsonRegularExpression(parameters.Address, "mi"));
}
if (!string.IsNullOrEmpty(parameters.Age))
{
filter &= Builders<Logs>.Filter.Regex(
"Age",
new BsonRegularExpression(parameters.Age, "mi"));
}
return filter;
}
This is my model class
public class Parameters
{
public string Name { get; set; }
public string Address { get; set; }
public string Age { get; set; }
}
Here startDate and endDate are not null. Always I am passing the current date from frontend.
I have to use dictionary to get the input parameters. Can someone help?
Thank you!!
Currently I have a method to create a filter which filters by name, address and age using regex in my c# backend. I want to remove the if conditions and use a loop instead. How can I do that?
This is my current method.
private static FilterMethod<Logs> GetFilter(Parameters parameters, DateTime startDate, DateTime endDate)
{
var filter = Builders<Logs>.Filter.Gte(x => x.Date, startDate);
filter &= Builders<Logs>.Filter.Lte(x => x.Date, endDate);
if (!string.IsNullOrEmpty(parameters.Name))
{
filter &= Builders<Logs>.Filter.Regex(
"Name",
new BsonRegularExpression(parameters.Name, "mi"));
}
if (!string.IsNullOrEmpty(parameters.Address))
{
filter &= Builders<Logs>.Filter.Regex(
"Address",
new BsonRegularExpression(parameters.Address, "mi"));
}
if (!string.IsNullOrEmpty(parameters.Age))
{
filter &= Builders<Logs>.Filter.Regex(
"Age",
new BsonRegularExpression(parameters.Age, "mi"));
}
return filter;
}
This is my model class
public class Parameters
{
public string Name { get; set; }
public string Address { get; set; }
public string Age { get; set; }
}
Here startDate and endDate are not null. Always I am passing the current date from frontend.
I have to use dictionary to get the input parameters. Can someone help?
Thank you!!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.