【发布时间】:2014-12-03 17:38:27
【问题描述】:
如何从成员表达式中提取值,其中成员表达式中的表达式不是常量,而是参数表达式。
我正在为我们公司构建一个小型 Linq to MDX ORM。
在我的生成器模板中,在数据库中找到的每个维度都是一个类,并且在每个维度中,都有为该维度找到的 Attribute 属性。在生成所有维度类之后,将生成更高级别的Cube 类,其中包含作为属性的维度以及该多维数据集的度量。生成所有多维数据集类后,将构建包含作为Cube<MyCube> 属性生成的多维数据集类的最终类,其中Cube<> 是我的IQueryable 类型。这是一个例子。
//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
}
现在,一个非常基本的 linq 查询示例来查询多维数据集:
//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
}
例如,我试图从 cube.Calendar.Month.Children 中获取值,该值来自 Attribute 对象,该对象是 FooCube_Calendar_Demsion 类的属性,它本身就是“FooCube”类中的属性.
我尝试了Access the value of a member expression 的答案,但在尝试编译 lambda 表达式时出现错误“未引用 'cube' 参数”。它传递给属性类的构造函数的值存储在一个属性中,这就是我想要访问的值(其中之一)。
【问题讨论】:
-
您想从该查询中获得什么价值,具体而言?该查询中有很多成员表达式;你需要清楚你要评估哪些。
-
Attribute 类中有一个“Tag”属性。这就是“[Calendar].[Month]”字符串的存储位置。还有一个“Children”属性,它简单地将“.CHILDREN”连接到值并返回“[Calendar].[Month].CHILDREN。这些是我想要的值。
-
您应该为您的属性使用 C# 属性,以获取有关在创建查询时使用的属性的信息。从属性返回的值应该是查询的结果,而不是创建查询时使用的值。当您的属性在其上使用 C# 属性时,您可以检查这些值,而无需拥有这些类型的特定实例。在这里以 EF 为例。
-
这就是我所采用的方法,例如 [Tag(Tag="[Calendar].[Month]")],效果很好,但我也想让用户能够添加在另一个部分类中查询范围内的用户成员和集合,以便他们可以在 linq 查询中调用它们,我希望他们能够使用生成的对象来这样做。此外,我希望用户能够使用 linq 查询中的“let”表达式即时添加这些计算成员。能够做到这一点的唯一方法是以某种方式从这些挑剔的成员表达式中提取值。
标签: c# linq expression mdx expression-trees