0
votes

Does Breeze have something like .SingleOrDefault()?

I'm getting my feet wet with Breezejs -- really cool! I have a case where I am using a parameterized query (so I'm using .withParameters() in my Breeze query).

My query will always return a single entity. In fact, it's defined like so on the WebApi controller:

public MyEntity GetThisSpecialEntity(int theParameter) {}

(note that it doesn't return IQueryable or IEnumerable, but just the entity.

The the query executes, the data that comes back from breeze appears to be an array of 1, with my entity in it.

Now I could just take data[0] and assign it, but I was wondering if that's what I'm supposed to do, or if Breeze has something like LINQ's .SingleOrDefault() method?

2

2 Answers

0
votes

You can very much return just a single entity. You just need to return the single result when your manager executes the get query like below:

For example, if you have an executeGetQuery function in your datacontext service, where you pass in your query object:

var executeGetQuery = function (query, observable) {
return manager.executeQuery(query).then(function (data) {
if (observable) {
observable(data.results[0]);    // Do it here
}

Hope it helps.

1
votes

Yep, the standard result of a Breeze query is an array, and it is up to the developer to write the query results to whatever they would like. In your situation, as demonstrated on the Breeze website, simply use data.results[0]