0

I am playing with a basic implementation of the Query Object "pattern" (in quotes since this is an implementation of the interpreter pattern), and I was thinking of an enhancement which I do not know how to implement.

As you know a Criterion object would look something like the following:

public class Criterion
{
  ...
  string FieldName { get; set; }
  string Value { get; set; }
  ....
}

What I do not like is the fact that I have to add the field a string so I was thinking if I could create some kind of generic Criterion class so I could use it like so:

var criterion = new Criterion<Person>();
criterion.Field = c => c.FirstName;
criterion.Value = "John";

or something along those lines.

Of course there also would be the part where the criteria needs to be converted into SQL so I could do something like:

string query = "SELECT * FROM Person WHERE " + criteria.Field.ToString() + " = '" + criteria.Value + "'"

I am sure there's got to be a way of doing this but I just can't wrap my head around it.

Thanks for your help.

Sergio Romero
  • 6,477
  • 11
  • 41
  • 71
  • possible duplicate of [Get method name and type using lambda expression](http://stackoverflow.com/questions/273941/get-method-name-and-type-using-lambda-expression) – nawfal Apr 27 '13 at 12:41

1 Answers1

0

You can pretty easily do this with Expressions. This article has an example - Get property name and type using lambda expression

Community
  • 1
  • 1
Nikolay
  • 3,658
  • 1
  • 24
  • 25