views:

47

answers:

2

I use Ria Service domainservice for data query.

In My database, there is a table People with firstname, lastname. Then I use EF/RIA services for data processing.

Then I create a Filter ViewModel to capture user inputs, based it the input, I construct a linq Query to access data.

At server side, the default DomainService query for person is:

 public IQueryable<Person> GetPerson()
   {
     return this.Context.Person;
   }

At client side, the linq Query for filter is something like(I use Contains function here):

if (!String.IsNullOrEmpty(this.LastName))
    q = q.Where(p => (p.LastName.Contains(this.LastName)));

The generated linq query is something like(when debugging,I got it):

MyData.Person[].Where(p => (p.LastName.Contains(value(MyViewModel.PersonFilterVM).LastName) || p.Person.LegalLastName.Contains(value(MyViewModel.PersonFilterVM).LastName)))

When I run the app, I put "Smith" for last name for search, but the result is totally irrelevant with "Smith"!

How to fix it?

+1  A: 

I'm guessing here as to what your error is so this might not work for you.

In your 2nd code snippet you do the following.

q = q.Where(p => (p.LastName.Contains(this.LastName)));

This is where I think your error is. Linq does not evaluate the where clause until you iterate over it. Try changing the line to the following.

qWithData = q.Where(p => (p.LastName.Contains(this.LastName))).ToList();

The .ToList() call will load the query with data.

Jason
A: 

When you check in the debugger, does value(MyViewModel.PersonFilterVM).LastName evaluate to Smith at the time the query is resolved?

Recall that queries are not resolved until they are enumerated.

David B