khadi8
khadi8

Reputation: 65

method with optional parameter

I have a method that has four parameters that I use to filter my data, sometimes the 4 parameters are filled and sometimes only three or two or one are filled, so I was looking for a way to take the necessary parameter values only. For example, if the user enter just one parameter like startDate I want to recover all the data with that date without taking other parameters as null, but it should not take them into consideration, my method searches the data which have the correct StartDate and the other parameters null I don't want do this

internal static List<Inconsistence> FilterList(DateTime? StartDate, DateTime? EnDate, decimal? State, decimal? Type)
{
    using (Model m = new Model())
    {
        return m.DBContext
                .InconsistencyDebtors
                .Join(m.DBContext.Inconsistencies,
                      u => u.InconsistencyId, 
                      uir => uir.InconsistencyId,
                      (u, uir) => new { u, uir })
                .Join(m.DBContext.InconsistencyDebtorDocuments,
                      r => r.uir.InconsistencyId, 
                      ro => ro.InconsistencyId, 
                      (r, ro) => new { r, ro })
                .Where(g => 
                       g.r.uir.InconsistencyStateId == State &&
                       g.r.uir.InconsistencyTypeId == Type  && 
                       g.r.uir.InsDate >= StartDate && 
                       EnDate >= g.r.uir.InsDate)
                .Select(g => new Inconsistence()
                   {
                       ParticipantCode = g.r.u.ParticipantCode,
                       DebtorId = g.ro.DebtorId,
                       InconsistencyTypeId = g.r.uir.InconsistencyTypeId,
                       InconsistencyStateId = g.r.uir.InconsistencyStateId,
                       DateInconsistence = g.r.uir.InsDate
                   })
                .ToList();
    }
}

Upvotes: 1

Views: 1261

Answers (4)

Daniele
Daniele

Reputation: 1938

I think you can write your where clause like this:

.Where( g =>
  (State.HasValue.Equals(false) || g => g.r.uir.InconsistencyStateId == State) && 
  (Type.HasValue.Equals(false) || g.r.uir.InconsistencyTypeId == Type) &&
  (StartDate.HasValue.Equals(false) || g.r.uir.InsDate >= StartDate) &&
  (EnDate.HasValue.Equals(false) || EnDate >= g.r.uir.InsDate)
)

I didn't tested it, but it should work.

Upvotes: 0

musium
musium

Reputation: 3072

You could make your parameters optional: MSDN

internal static List<Inconsistence> FilterList(DateTime? StartDate, DateTime? EnDate = null, decimal? State = null, decimal? Type = null)

Upvotes: 0

VladL
VladL

Reputation: 13033

params could do that for you, but you will need to down cast the values to object

Define the function as

internal static List<Inconsistence> FilterList(params object[] list)
{
   //check the size of `list` object and cast the values
   (DateTime)list[0]
   (double)list[1]
}

and the call would be

FilterList(DateTime.Now);
FilterList(DateTime.Now, 1.0);

Upvotes: 0

Patrik
Patrik

Reputation: 1372

Option 1: Overload. Create 4 methods, each with a different signature which call the "main"-method, e.g. the method with full parameters. The Call goes along with some default parameters.

internal static List<Inconsistence> FilterList(DateTime? StartDate, DateTime? EnDate, decimal? State)
{
    return FilterList(StartDate, EnDate, State, null); // Call overloaded method with Type = null
}

internal static List<Inconsistence> FilterList(DateTime? StartDate, DateTime? EnDate, decimal? State, decimal? Type)

Option 2: Default-values You can give your method-parameter a default-value. Only the last parameters can have default values. Looks like this

internal static List<Inconsistence> FilterList(DateTime? StartDate, DateTime? EnDate, decimal? State, decimal? Type = null)

This, the Type-parameter would be optional. When not specified in the call, it will have the assigned default value. In this case, it is null.

Upvotes: 3

Related Questions