Reputation: 31
First of all, apologies if I posted it in the wrong place, I'm new here and I'm not sure if I posted in the right place. Well, I'm trying to build a generic search method, where I'll add search parameters to mount a SQL Query and execute it on the database. All that using C#. My goal is that the parameter corresponding to the field I'll search, to be a property of the class the method is in. For example:
public foo
{
public string CustomerCode { get; set; }
public string CustomerName { get; set; }
public void AddSearchParameter(???, EnumOperator Operator, object Value)
}
Whenever I want to specify a parameter to add on the search, I would like it to look like this:
foo xxx = new foo();
xxx.AddSearchParameter(foo.CustomerCode, EnumOperator.Equal, txtCustomerCode.text);
My question is how to do it?
Upvotes: 2
Views: 113
Reputation: 1062484
If you are trying to pass the member information (so that the AddSearchParameter
can inspect the MemberInfo
and write suitable SQL), then you'd need to use either a string literal (i.e. "CustomerCode"
), or an expression tree. The latter is richer, but involves learning the Expression<T>
API. But fundamentally:
public void AddSearchParameter(Expression<Func<object>> expression, ...)
...
xxx.AddSearchParameter(() => foo.CustomerCode, ...)
This, however, is not a trivial area of .NET.
Upvotes: 3
Reputation: 15503
If I were doing something like this, I would probably make the Search()
method on foo
check for the existence of values in the various this
properties, and then build the query based on that.
public List<Results> Search()
{
if (!String.IsNullOrEmpty(this.CustomerCode))
{
// add search value to query
}
// etc.
}
Upvotes: 1