I have a requirement to get a list of different values ββfor specific properties of a collection of objects.
So, let table A have fields x, y, z, 1, 2, 3, where x is PK (thus, outside the table).
I need to get all the unique values ββfor y, z, 1, 2, or 3, without knowing in my method which field I get. Thus, the template for the method will look like this:
public List<ObjectName> GetUniqueFieldValues(string fieldname)
An ObjectName is an object with two properties that the above method populates at least one property for each result.
Someone in another question had a similar answer using the ParameterExpression and Expression classes, but in fact they did not provide enough information to help me with my specific task.
I was also looking for reflection, but of course Linq doesn't really like the Select statement.
I would just use it, if I call it good, but there is really a ton of fields / properties in a real table / object, so this is impractical. It will also save me some refactoring if the base table ever changes.
SQL version of what I'm trying to do:
SELECT Distinct [usersuppliedfieldname] from TableName where [someotherconditionsexist]
The pseudocode of what I already have:
public List<ReturnObject> GetUniqueFieldValues(int FkId, ConditionObject searchmeta) { using(DbEntities db = new DbEntities()) { // just getting the basic set of results, notice this is "Select *" var results = from f in db.Table where f.FkId == FkId && [some static conditions] select f; // filtering the initial results by some criteria in the "searchmeta" object results = ApplyMoreConditions(results, searchmeta); // GOAL - Select and return only distinct field(s) specified in searchmeta.FieldName) } }