Question

I have a problem when using LINQ to set a where clause on a query in a domain service call.

If I don't set a where clause it works just fine and I get my result back in my completed event (I have only implemented the eventhandler for the completed event to demonstrate my issue here).

LoadOperation<User> load = 
  _Context.Load(_Context.GetUsersQuery());
load.Completed += (se, ea) => 
  MyList.ItemsSource = load.Entities.FirstOrDefault();

If I put a where clause on the query I simply get an empty list. And the where clause should return 1 row :)

LoadOperation<User> load = 
  _Context.Load(_Context.GetUsersQuery().Where(f => f.UserID == UserID));
load.Completed += (se, ea) => 
  MyList.ItemsSource = load.Entities.FirstOrDefault();

Any feedback would be appreciated!

Was it helpful?

Solution

AFAIK I don't believe it is possible to put the where clause on RIA Services queries like that. You need to implement a method in your service that takes the UserID and puts the where clause on the query in the service.

Here is an example from Brad Abram's blog series on the subject:

public IQueryable<SuperEmployee> GetSuperEmployee(int employeeID)
{
    return Context.GetEmployees().ToList()
          .Where(emp => emp.EmployeeID == employeeID).AsQueryable();

}

OTHER TIPS

According to this post

The "in" is not a valid syntax when calling RIA services. However, you can build a predicate expression that does exactly what "in" would do.

Usage:

var ids = new[] { 10, 20,30};
var inExpression = BuiltInExpression (ids);

EntityQuery<Product> listQuery = 
  ProductContext.GetProductsQuery().Where(inExpression);

Supporting code:

static Expression<Func<Product, bool>> BuiltInExpression(int[] ids) 
{ 
 var inputExpression = Expression.Parameter(typeof(Product), "input"); 
 var valueExpression = Expression.Property(inputExpression, s_propertyInfo); 
 var inExpression = 
 ids 
 .Aggregate( 
 default(Expression), 
 (acc, v) => 
 { 
  var vExpression = Expression.Constant(v, typeof(int?)); 
  var compareExpression = Expression.Equal(valueExpression, vExpression); 
  if (acc == null) 
  { 
   return compareExpression; 
  } 
  return Expression.OrElse(acc, compareExpression); 
 }); 

 inExpression = inExpression ?? Expression.Constant(true); 
 return Expression.Lambda<Func<Product, bool>>( 
  inExpression, 
  inputExpression 
 );

 return inExpression;

}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top