IQueryable to List
我知道 IQueryable 不会产生任何结果,而只是一个表达式生成器,我的问题是如何实际使用它来执行查询并将集合返回为列表,以便能够将其绑定在网格上。
IQueryable query = _campaignManager.GetCampaign(filter, values);
// this line returns error
List<Campaign> campaigns = query.Cast<Campaign>().ToList();
grdCampaigns.DataSource = campaigns;
grdCampaigns.DataBind();
其他详细信息: GetCampaign()
public IQueryable GetCampaign(string filter, params object[] values)
{
string parameters = string.Empty;
foreach (object obj in values)
{
parameters += obj.ToString() + ",";
}
parameters.Remove(parameters.Count() - 1, 1);
var query = context.Campaigns.Where(filter, parameters)
.Select("new(CampaignID,CampaignName)");
return query;
}
我正在使用 DynamicQueryable 用于动态 linq 查询
DynamicQueryable
public static IQueryable Select(this IQueryable source, string selector, params object[] values)
{
if (source == null) throw new ArgumentNullException("source");
if (selector == null) throw new ArgumentNullException("selector");
LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, null, selector, values);
return source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Select",
new Type[] { source.ElementType, lambda.Body.Type },
source.Expression, Expression.Quote(lambda)));
}
IQueryable .Where() 扩展
public static IQueryable Where(this IQueryable source, string predicate, params object[] values)
{
if (source == null) throw new ArgumentNullException("source");
if (predicate == null) throw new ArgumentNullException("predicate");
LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
return source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Where",
new Type[] { source.ElementType },
source.Expression, Expression.Quote(lambda)));
}
的 .Select 扩展方法感谢...
I know IQueryable yields no result but just an expression builder, my problem is how can actually use it to execute query and return the set as List to be able to bind it on a grid.
IQueryable query = _campaignManager.GetCampaign(filter, values);
// this line returns error
List<Campaign> campaigns = query.Cast<Campaign>().ToList();
grdCampaigns.DataSource = campaigns;
grdCampaigns.DataBind();
additional details: GetCampaign()
public IQueryable GetCampaign(string filter, params object[] values)
{
string parameters = string.Empty;
foreach (object obj in values)
{
parameters += obj.ToString() + ",";
}
parameters.Remove(parameters.Count() - 1, 1);
var query = context.Campaigns.Where(filter, parameters)
.Select("new(CampaignID,CampaignName)");
return query;
}
I'm using DynamicQueryable for dynamic linq queries
The .Select Extension method of the DynamicQueryable
public static IQueryable Select(this IQueryable source, string selector, params object[] values)
{
if (source == null) throw new ArgumentNullException("source");
if (selector == null) throw new ArgumentNullException("selector");
LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, null, selector, values);
return source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Select",
new Type[] { source.ElementType, lambda.Body.Type },
source.Expression, Expression.Quote(lambda)));
}
IQueryable .Where() extension
public static IQueryable Where(this IQueryable source, string predicate, params object[] values)
{
if (source == null) throw new ArgumentNullException("source");
if (predicate == null) throw new ArgumentNullException("predicate");
LambdaExpression lambda = DynamicExpression.ParseLambda(source.ElementType, typeof(bool), predicate, values);
return source.Provider.CreateQuery(
Expression.Call(
typeof(Queryable), "Where",
new Type[] { source.ElementType },
source.Expression, Expression.Quote(lambda)));
}
thanks...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 .NET 4.0 并对动态库稍加修改,我们可以获得预期结果:
varcampaigns = query.Cast().ToList();
甚至var活动 = query.ToList();
操作方法如下:
更改:
至:
这是使用修改后的库的工作代码:
我们还可以向 DynamicQueryable 类:
那么我们应该能够像这样调用:
PS:来自 MSDN:
因此,我们应该能够在不进行建议的更改的情况下调用
Cast()
。With .NET 4.0 and a slight modification to the Dynamic library, we can achieve the expected result for:
var campaigns = query.Cast<dynamic>().ToList();
or evenvar campaigns = query.ToList();
Here is how to do it:
Change:
to:
Here is a working code using the modified library:
We may also add an overloaded extension method to the DynamicQueryable class:
Then we should be able to call like this:
P.S: From MSDN:
So we should be able to call
Cast<dynamic>()
without the proposed change.IQueryable 可以是 T 类型,例如
IQueryable query=+CampaignManager.GetCampaign
但由于您使用的是 IQueryable,您可以使用
我已经尝试过它正在工作,您可以继续使用它。
IQueryable can be of type T e.g.
IQueryable query=+CampaignManager.GetCampaign
but since you are using IQueryable you can use
i have tried that it's working you can proceed wit it.