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...
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.
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.