C# 表达式树作为属性的一部分

C# 表达式树作为属性的一部分,c#,linq,c#-4.0,C#,Linq,C# 4.0,我从这个问题中得到启发: 原始海报询问如何将其转换为表达式树,并得到了一个很好的答案,可以在上面的链接中看到 List<Region> lst = (from r in dc.Regions where r.RegionID > 2 && r.RegionDescription.Contains("ern") select r).ToList(); 我将如何定义AwesomePr

我从这个问题中得到启发:

原始海报询问如何将其转换为表达式树,并得到了一个很好的答案,可以在上面的链接中看到

List<Region> lst = (from r in dc.Regions
                    where r.RegionID > 2 && r.RegionDescription.Contains("ern")
                    select r).ToList();

我将如何定义
AwesomeProperty

您将定义
AwesomeProperty
,就像LINQ to SQL对象上的任何其他属性一样。假设它被键入为
bool
(因为您将它与
true
进行比较),您将构建
Where
查询,如下所示:

// Build the parameter to the where clause predicate and access AwesomeProperty

var regionParameter = Expression.Parameter(typeof(Region), "region");

var awesomeProperty = Expression.Property(regionParameter, "AwesomeProperty");

// Build the where clause predicate using the AwesomeProperty access

var predicate = Expression.Lambda<Func<Region, bool>>(awesomeProperty);

// Get the table, which serves as the base query

var table = dc.Regions.AsQueryable();

// Call the Where method using the predicate and the table as the base query

var whereCall = Expression.Call(
    typeof(Queryable),
    "Where",
    new[] { table.ElementType },
    table.Expression,
    predicate);

// Get an IQueryable<Region> which executes the where call on the table

var query = table.Provider.CreateQuery<Region>(whereCall);

var results = query.ToList();
//构建where子句谓词的参数并访问AwesomeProperty
var regionParameter=表达式参数(typeof(Region),“Region”);
var awesomeProperty=Expression.Property(regionParameter,“awesomeProperty”);
//使用AwesomeProperty访问构建where子句谓词
var谓词=Expression.Lambda(awesomeProperty);
//获取用作基本查询的表
var table=dc.Regions.AsQueryable();
//使用谓词和表作为基本查询调用Where方法
var whereCall=Expression.Call(
类型(可查询),
“哪里”,
新[]{table.ElementType},
表1.表达式,
谓词);
//获取一个IQueryable,它执行表上的where调用
var query=table.Provider.CreateQuery(whereCall);
var results=query.ToList();

您将定义
AwesomeProperty
,就像LINQ to SQL对象上的任何其他属性一样。假设它被键入为
bool
(因为您将它与
true
进行比较),您将构建
Where
查询,如下所示:

// Build the parameter to the where clause predicate and access AwesomeProperty

var regionParameter = Expression.Parameter(typeof(Region), "region");

var awesomeProperty = Expression.Property(regionParameter, "AwesomeProperty");

// Build the where clause predicate using the AwesomeProperty access

var predicate = Expression.Lambda<Func<Region, bool>>(awesomeProperty);

// Get the table, which serves as the base query

var table = dc.Regions.AsQueryable();

// Call the Where method using the predicate and the table as the base query

var whereCall = Expression.Call(
    typeof(Queryable),
    "Where",
    new[] { table.ElementType },
    table.Expression,
    predicate);

// Get an IQueryable<Region> which executes the where call on the table

var query = table.Provider.CreateQuery<Region>(whereCall);

var results = query.ToList();
//构建where子句谓词的参数并访问AwesomeProperty
var regionParameter=表达式参数(typeof(Region),“Region”);
var awesomeProperty=Expression.Property(regionParameter,“awesomeProperty”);
//使用AwesomeProperty访问构建where子句谓词
var谓词=Expression.Lambda(awesomeProperty);
//获取用作基本查询的表
var table=dc.Regions.AsQueryable();
//使用谓词和表作为基本查询调用Where方法
var whereCall=Expression.Call(
类型(可查询),
“哪里”,
新[]{table.ElementType},
表1.表达式,
谓词);
//获取一个IQueryable,它执行表上的where调用
var query=table.Provider.CreateQuery(whereCall);
var results=query.ToList();