2

I was unsure if what I wrote as a Title would suffice, but I will do my best to explain what I am trying to accomplish here:

I am trying to recreate this query that I use to look up distinct data from TestColumn:

 List<string> groupQuery = (from p in testDepo
                                      group p by new { p.TestColumn}
                                      into distinctTest
                                      select distinctTest.First().TestColumn).ToList();

But instead of statically call TestColumn. I would like to dynamically pass in the name: TestColumn into a variable and then do my distinct value query using the variable name.

So far I have this:

string primaryColumn = "TestColumn"
List<string> groupQuery= (from p in testDepo
                                    group p by new { primaryColumn }
                                    into distinctTest
                                    select distinctTest.First().GetType().GetProperty(primaryColumn).GetValue(distinctTest.First()).ToString()).ToList();

I know i am probably doing something silly, but i cant quite wrap my head around it right now. my dynamic query only half works. It is only getting the first distict value, but not the rest of them. Can anyone help me out?

5
  • What is the type of p in your query? Commented Apr 24, 2015 at 20:30
  • p is the object that i am trying get distinct values from. Commented Apr 24, 2015 at 20:36
  • Yes, but what is the type of that object? Commented Apr 24, 2015 at 20:40
  • Like my entity model that is connected to my database to a table called Test Commented Apr 24, 2015 at 20:54
  • Nice coincidence, I just named the test class in my code Test just like your table name. Commented Apr 24, 2015 at 21:08

2 Answers 2

4

You can create a Func from an Expression using your property column string like this:

var primaryColumn = "TestColumn";
var param = Expression.Parameter(typeof(Test), "p");
var col = Expression.Property(param, primaryColumn);
var lambda = Expression.Lambda(col, param);
var func = lambda.Compile();

Then use it in your query like this:

List<string> groupQuery= (from p in testDepo
                          group p by func.DynamicInvoke(p)
                          into distinctTest
                          select distinctTest.First()
                                             .GetType()
                                             .GetProperty(primaryColumn)
                                             .GetValue(distinctTest.First())
                                             .ToString()
                          ).ToList()
Sign up to request clarification or add additional context in comments.

2 Comments

for var p = Expression.Parameter(typeof(Test), "p"); i would not be able to use p since it is already being used in my query. Would i have to change both the variable name and the string name? or just the variable name?
Change just the variable name. I fixed the answer.
0

What is TestDepro? You need to be able to return the column by name something like p[primaryColumn].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.