質問

In LightSwitch I have a PreprocessQuery event as follows:

partial void ValidOrders_PreprocessQuery(ref IQueryable<Order> query)
{
    query = query.Where(order => OrderIsValid(order));
}

public bool OrderIsValid(Order order)
{
    return true;
}

This fails with a message (on the HTMLClient side !) "method cannot be null".

But this works fine:

partial void ValidOrders_PreprocessQuery(ref IQueryable<Order> query)
{
    query = query.Where(order => true);
}

Can someone see why?

Thanks, Paul

役に立ちましたか?

解決

The query provider is only shown the method OrderIsValid, and as that method has already been compiled down to IL it can no longer "look into it" to see it's implementation, as it would need to to create Expression objects to represent it.

There are a number of options that you have, ranging from inlining the method as you did yourself, or having the method itself return an expression, rather than doing the work:

public Expression<Func<Order, bool>> OrderIsValid()
{
    return order => true;
}

This would let you write:

partial IQueryable<Order> ValidOrders_PreprocessQuery(IQueryable<Order> query)
{
    return query.Where(OrderIsValid());
}

As a side note, I would strongly advise you to not pass the query by reference, but rather return a new query instead; that would be the more idiomatic approach.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top