Search code examples
breeze

Getting Distinct or FirstOrDefault with Breeze Client-Side


A query already executed with breeze on the client side and I store the results locally. I was really hoping to either .Distinct() or .FirstOrDefault() on the client side so that I can execute locally instead of going back to the server.

I know that breeze has limited capabilities on the client side. I've looked through the samples and no luck there. http://www.breezejs.com/documentation/query-examples

Can breeze do this? Is this something that they will be doing in the future?


Solution

  • I need to see your query to answer properly.

    In almost all cases, take(1) is equivalent to firstOrDefault and works fine on both remote and local queries.

    var query = breeze.EntityQuery.from('Persons')
          .where('FirstName', 'eq', 'Lizzy')
          .take(1); // will return one or null
    

    If you want to find an entity by its key, looking first in the cache and then going remote if necessary, then you should consider the fetchEntityByKey method.

    Distinct only makes sense for projection queries in which the objects returned (which are NOT entities, btw) have no id. Can't know if that is what you need until you show me your query.

    With due respect, I feel that hunch_hunch's answer is a bit misleading. Please reconsider checking MY ANSWER as the best answer.