AsEnumerable 和查询语法
AsEnumerable and Query Syntax
我有一个使用查询语法编写的相当复杂的 Linq 表达式。简化后,它看起来像:
var query =
from a in ctx.A
from b in ctx.B
where a.Bid == b.Id
select new MyType()
{
Foo = a.Foo,
Bar = b.Bar
}
我必须修改查询,以查询提供程序无法评估的方式在 MyType 的新实例上设置 属性。结果集相当小,因此使用 .AsEnumerable() 是合理的。我找到了一个 example on MSDN,它展示了如何在一个简单的案例中做到这一点
IEnumerable<DataRow> query =
from product in products.AsEnumerable()
select product;
如何在更复杂的情况下使用 AsEnumerable()?也就是说,我如何实现
var query =
from a in ctx.A
from b in ctx.B
where a.Bid == b.Id
AsEnumerableGoesHereSomehow
select new MyType()
{
Foo = a.SomeClientSideCalculation(),
Bar = b.Bar
}
我不想在客户端执行加入和过滤。
随意将 {a=a,b=b} 减少到您实际需要的属性,但您可以以此作为开始:
var query =(
from a in ctx.A
from b in ctx.B
where a.Bid == b.Id
select new
{
a = a,
b = b
})
.AsEnumerable()
.Select(x=>new MyType {
Foo=Something(x.a),
Bar=x.b
});
我有一个使用查询语法编写的相当复杂的 Linq 表达式。简化后,它看起来像:
var query =
from a in ctx.A
from b in ctx.B
where a.Bid == b.Id
select new MyType()
{
Foo = a.Foo,
Bar = b.Bar
}
我必须修改查询,以查询提供程序无法评估的方式在 MyType 的新实例上设置 属性。结果集相当小,因此使用 .AsEnumerable() 是合理的。我找到了一个 example on MSDN,它展示了如何在一个简单的案例中做到这一点
IEnumerable<DataRow> query =
from product in products.AsEnumerable()
select product;
如何在更复杂的情况下使用 AsEnumerable()?也就是说,我如何实现
var query =
from a in ctx.A
from b in ctx.B
where a.Bid == b.Id
AsEnumerableGoesHereSomehow
select new MyType()
{
Foo = a.SomeClientSideCalculation(),
Bar = b.Bar
}
我不想在客户端执行加入和过滤。
随意将 {a=a,b=b} 减少到您实际需要的属性,但您可以以此作为开始:
var query =(
from a in ctx.A
from b in ctx.B
where a.Bid == b.Id
select new
{
a = a,
b = b
})
.AsEnumerable()
.Select(x=>new MyType {
Foo=Something(x.a),
Bar=x.b
});