可以列出<t> </t>

时间:2011-05-13 09:37:30

标签: c# linq iqueryable

我知道IQueryable不会产生任何结果,只会产生表达式构建器,我的问题是如何实际使用它来执行查询并将集合作为List返回以便能够在网格上绑定它。

  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的.Select扩展方法

     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)));
    }

感谢...

2 个答案:

答案 0 :(得分:2)

使用.NET 4.0并对动态库稍作修改,我们可以实现以下预期结果:var campaigns = query.Cast<dynamic>().ToList();甚至var campaigns = 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));


P.S:来自MSDN

  

任何对象都可以隐式转换为动态类型。

因此,我们应该能够在没有建议更改的情况下致电Cast<dynamic>()

答案 1 :(得分:0)

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    
            }

我已经尝试过它的工作,你可以继续机智。