开发者

WCF RIA Services in SL List.count() get the total number of items despite an EntityQuery composed on client side

I have a SL4 application, that uses the WCF RIA services and LINQ2SQL.

I compose an EntityQuery on client side, and want to get the number of items in the list on server side, but the List.count() doesn't takes into account the set EntityQuery object. Shows the total number of items, but send the correct number of items to the client.

The DomainService method:

public IQueryable<Invoice> GetMyInvoices(string userID, int numberInCache)
    {
        IQueryable<Invoice> myList = this.D开发者_如何学PythonataContext.Invoices.Where(s => s.userID == userID);

        if (numberInCache > 0)
        {
            if (myList.Count() == numberInCache)
            {
                return null;
            }
        }
        return myList;
    }

On Client side:

EntityQuery<Invoice> query = myDataContext.GetMyInvoicesQuery(userID, numberInCache).Where(i => i.deliveryDate.month == 4);

        LoadOperation<Invoice> lo = tetelSearchContext.Load(query,
            (result) =>
            {
                if (!result.HasError)
                {
                    foundTetels = result.Entities;
                }
                else
                {
                    MessageBox.Show(result.Error.Message);
                    result.MarkErrorAsHandled();
                }
            }
        , null);

In the client side I get the correct list's count, but in the server side on doesn't takes into account the deliveryDate.month filter.

What did I wrong?

Thanks in advance

Gabor


Thank You for the clarification.

In my situation the application has to foresee when the requested objects are subset of the entityset on the client side.

I thought, the simplest way to decide, to compare the object's count after filtering the EntitySet on the client and applying the same filter on the server.

The application has to download the records from the server, only, when the counts differ.

In output caching, one will reload the objects from the server in every case, when the filter has new params, despite the records are on the client, because they are a subset of the previous result set.

For example: request the invoices of the first quarter, then request invoices in January.

Because I have numerous conditions, it isn't clear if the client contains all the records on the client, or not. (If we request all invoices in January, after got paid invoices in the first quarter, the client entityset will contains all the records only, if all invoices are paid in January.)

So I thought, it's more efficient to get the number of objects on the server first, and then download the EntitySet if needed.

Anybody has a better solution?

Thanks in advance,

Gabor.


Well, first let me tell you how RIA services work.

  1. RIA Client composes query
  2. RIA Client sends some complex XML to server
  3. RIA Service (On Server side) first calls GetMyInvoices
  4. Please note, your client's query is not applied yet
  5. RIA Service then takes return value of your query returned by method GetMyInvoices
  6. RIA Services now applies XML sent by client
  7. RIA Services returns the result to client

So now look at your step 3, in your GetMyInvoices you are applying a small query on your Context, regardless of what client sent you. Your client's composed query filter is applied at the last step.

Instead, you have to call two queries one after another at client side, 1. First fetch the results count 2. If it does not match then fetch your records

However out of curiosity, whey do you care to see anything on server side at all? Let server send you results of date query directly, as you can implement paging etc, you will not get great performance by splitting query like this and manage client side and server side query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜