1

I'm currently struggeling with building dynamic queries using LINQKit.

For my particular entity User I have the following property defined:

public static Expression<Func<User, MyEntites, int>> CountFromView => (user, ctx) => ctx.vwUserStats.Select(s => s.Count).FirstOrDefault();

This I would normally use in linq like Select(u => new {Count = User.CountFromView.Invoke(u, ctx)} which works totally find. However building the queries is more complex than I could write it down here.

I have a static helper which builds the query depening on a given config (indicating what and how things should be selected/queried from database).

var selectType = typeof(UserstatsSelect);
var theEntity = Expression.Parameter(typeof(User));

if (cfg.Select(nameof(UserstatsSelect.Count)))
    memberBindings.Add(Expression.Bind(selectType.GetProperty(nameof(Count)), Expression.Invoke(User.EventCountFromView, theEntity, ContextParameter)));

whereas ContextParameter is defined in the base-class as followed:

protected static ParameterExpression ContextParameter => Expression.Parameter(typeof(MyEntites), "context");

When I run the app and let me show the query it shows

(context, Param_0) => new OrganizerStatisticsResourceSelect() {
    EventCount = Invoke((user, ctx) => ctx.vwUserStats.Select(s => s.Count).FirstOrDefault(), Param_0, context)
}

which seems totally fine. Unfortunately I get the exception

The parameter 'context' was not bound in the specified LINQ to Entities query expression

I have no idea why this is happening as I also have other such properties to which I pass the ContextParameter the same way an it works. Note: There I call Count() on the resulting set.

(context, Param_0) => new OrganizerStatisticsResourceSelect() {
    AdvancedCount = Invoke((user, dbContext) => dbContext.fnGetSomeData(Convert(user.Id)), Param_0, context).AsQueryable()).Count()
}
KingKerosin
  • 3,639
  • 4
  • 38
  • 77
  • Full sample of code needed... You are only posting code snippets, but we can't compile them in a full code. Your question is quite complex (and based on an external library, linqkit). We don't read minds nor predict the future nor look at the hd of the questioners. – xanatos May 23 '17 at 05:19

0 Answers0