IQueryable to List

发布于 2024-11-07 00:21:08 字数 2387 浏览 0 评论 0原文

我知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

空‖城人不在 2024-11-14 00:21:08

使用 .NET 4.0 并对动态库稍加修改,我们可以获得预期结果: varcampaigns = query.Cast().ToList(); 甚至 var活动 = query.ToList();

操作方法如下:
更改:

public abstract class DynamicClass {

至:

public abstract class DynamicClass : System.Dynamic.DynamicObject {

这是使用修改后的库的工作代码:

var query = db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10).
            OrderBy("CompanyName").
            Select("New(CompanyName as Name, Phone)");

foreach (var val in query.Cast<dynamic>().ToList())
     Console.WriteLine(string.Format("Name: {0}, Phone: {1}", val.Name, val.Phone));

我们还可以向 DynamicQueryable 类:

public static IQueryable<T> Select<T>(this IQueryable source, string selector, params object[] values)
{
    return Select(source, selector, values).Cast<T>();
}

那么我们应该能够像这样调用:

var query = db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10).
            OrderBy("CompanyName").
            Select<dynamic>("New(CompanyName as Name, Phone)");

foreach (var val in query.ToList())
    Console.WriteLine(string.Format("Name: {0}, Phone: {1}", val.Name, val.Phone));

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 even var campaigns = query.ToList();

Here is how to do it:
Change:

public abstract class DynamicClass {

to:

public abstract class DynamicClass : System.Dynamic.DynamicObject {

Here is a working code using the modified library:

var query = db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10).
            OrderBy("CompanyName").
            Select("New(CompanyName as Name, Phone)");

foreach (var val in query.Cast<dynamic>().ToList())
     Console.WriteLine(string.Format("Name: {0}, Phone: {1}", val.Name, val.Phone));

We may also add an overloaded extension method to the DynamicQueryable class:

public static IQueryable<T> Select<T>(this IQueryable source, string selector, params object[] values)
{
    return Select(source, selector, values).Cast<T>();
}

Then we should be able to call like this:

var query = db.Customers.Where("City == @0 and Orders.Count >= @1", "London", 10).
            OrderBy("CompanyName").
            Select<dynamic>("New(CompanyName as Name, Phone)");

foreach (var val in query.ToList())
    Console.WriteLine(string.Format("Name: {0}, Phone: {1}", val.Name, val.Phone));

P.S: From MSDN:

Any object can be converted to dynamic type implicitly.

So we should be able to call Cast<dynamic>() without the proposed change.

玻璃人 2024-11-14 00:21:08

IQueryable 可以是 T 类型,例如
IQueryable query=+CampaignManager.GetCampaign

但由于您使用的是 IQueryable,您可以使用

var enumerator= c.GetEnumerator();
            while (enumerator.MoveNext())
            {
             //add these records to some collection say Collection or Campaign or Create any entity with Name and Id and then assign that collection to DataSource    
            }

我已经尝试过它正在工作,您可以继续使用它。

IQueryable can be of type T e.g.
IQueryable query=+CampaignManager.GetCampaign

but since you are using IQueryable you can use

var enumerator= c.GetEnumerator();
            while (enumerator.MoveNext())
            {
             //add these records to some collection say Collection or Campaign or Create any entity with Name and Id and then assign that collection to DataSource    
            }

i have tried that it's working you can proceed wit it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文