How do I extract a value from a Member Expression where the expression within the Member Expression is not a Constant, but a Parameter Expression.
I am building a small Linq to MDX ORM for our company.
In my generator template, each Dimension found in the database is a class, and in each dimension, there are the Attribute
properties that are found for that dimension. After all the dimension classes are generated, a higher level Cube
class is generated that contains the Dimensions as properties, as well as the Measures for that cube. After all the cube classes are generated, a final class is built that contains the cube classes that were generated as Cube<MyCube>
properties, where the Cube<>
is my IQueryable
type. Here's an example.
//Generated cs file example:
public partial MyDatabase : CubeBase
{
//Some base implementation goes here
public Cube<FooCube> FooCube { get { return new Cube<FirstCube>(new Provider("connection string"); } }
//Any other cubes would follow here that were found on this database.
}
//A calendar dimension
public class FooCube_CalendarDimension
{
public Attribute WeekEnding { get { return new Attribute("[Calendar].[Week Ending]"); } }
public Attribute Month { get { return new Attribute("[Calendar].[Month]"); } }
public Attribute Year { get { return new Attribute("[Calendar].[Year]"); } }
}
//The "FooCube"
public partial class FooCube
{
//List Dimensions
public FooCube_Calendar_Dimension Calendar { get { return new FooCube_Calendar_Dimension(); } }
//Other dimensions here
[Measure]
public string RetailDollars { get { return "[Retail Dollars]"; } }
// Other measures here
}
Now, an example of a very basic linq query to query the cube:
//using MyDatabase = db
var mdx = from cube in db.FooCube
where cube.Calendar.Year == "2014"
select new
{
Month = cube.Calendar.Month.Children
Dollars = cube.RetailDollars
}
For example, I'm trying to get the value from cube.Calendar.Month.Children, which comes from the Attribute object that is a property of the FooCube_Calendar_Demsion class, that is in itself a property in the "FooCube" class.
I tried the answer from Access the value of a member expression, but I get the error, "the 'cube' parameter was not referenced" when it tries to compile the lambda expression. The value that it passes to the attribute class's constructor is stored in a property, and that's the value (one of them) that I want to access.
Basically, you can't. At least, not in any sensible way. Currently all that you have is a query. You don't actually have a collection of objects, you just have information about what you need to do to create those objects. It is the job of the query provider that you're currently in the process of writing to actually build the objects that the query defines and return them.
You've designed your program such that the query provider that creates the objects needs to have the already created objects already in order to properly build the query. It's impossible to already have the objects defined by the query that you haven't built yet. You've created a circular dependency for yourself.
It's important for you to provide the information needed to build the query somewhere other than in instances the objects that the query itself creates. Typically this is done with attributes on the properties, or by basing the query on the other existing C# metadata about the type itself. This type data exists, and is accessible to your query provider without needing any actual instances of the objects you're tasked with creating.