How to implement search using Breeze Sharp? - breeze

I'm currently trying to implement a search function for a table in a database that uses Breeze Sharp and Breeze Asp Net Core. In their docs they just say to do this.
// Customers whose name contains the word, "market"
var query = new EntityQuery<Customer>()
.Where(cust => cust.CompanyName.Contains('market'));
But I have done essentially that and still get an error. The error is from the JsonQueryExpressionVisitor VisitMethodCall function when ExecuteQuery is creating the resourcePath. It says
"The method 'Contains' is not supported"
Is there any viable way to implemenet a search feature using Breeze?

Related

OData Model not loaded?

I am using an OData model in my SAP UI5 application. I do the following:
var oView = this.getView();
this._oModel = oView.getModel();
which I thought loaded the model and allowed me to access whatever I needed to inside the model. However whenever I try to get a property using getProperty(path) it returns undefined. I know the path is correct because I used it elsewhere in my application and it worked okay. The model I am using is named "metadata.xml" if that helps.
If you read the documentation provided by SAP talking about OData services (I assume v2), you might find your answer there. Since you are using getProperty(), "you can only access single entities and properties with these methods. To access entity sets, you can get the binding contexts of all read entities via a list binding." Therefore you may want to a read() of the entity set first and then use the results of this read to access whatever property you want.
Link to doc: https://sapui5.hana.ondemand.com/#docs/guide/6c47b2b39db9404582994070ec3d57a2.html

Passing parameters to an OData (GET) method which returns a collection

I am using OData v3. How do I pass a parameter to an OData controller and return a collection? Example of what I am trying to do:
[EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
public IQueryable<ServiceInfoResult> Get([FromODataUri] int instanceId)
{
//etc
}
When I test this, I get this error:
No HTTP resource was found that matches the request URI 'http://localhost:30863/odata/ServiceInfoResultApi(1)'.
No action was found on the controller 'ServiceInfoResultApi' that matches the request.
I am aware of OData actions, but they are not a viable option on this case, because I need to get odata.count returned in the response, but no matter what I do, I cannot get that property returned when using OData actions (even when trying what was suggested here: Web API OData Inlinecount not working and here: Webapi odata expand with entity framework functions). So it would seem my only alternative was to create a new OData API controller for the ServiceInfoResult entity, so that I can avoid OData actions. However, as you can see above, passing in a parameter to a method that returns a collection seems to cause other errors.
Any solutions? And no, I can't update to OData v4, since that presents a whole host of other issues (it would take more time than I have and moreover, it doesn't support DateTime)
UPDATE
Thanks to #Fan Ouyang, I've discovered the reason for odata.count missing in the JSON payload is that I am not returning an entity type. If ServiceInfoResult were an entity in my database, odata.count would be returned. A bit silly that it's not being returned just because of that, but that's just how it is. I'm wondering if there's any workaround. For example, can I download the source code, change 1 line of code and use that? Otherwise, maybe it's time I started looking at OData v4. The project I have is quite big, so that's not a nice thought with the short amount of time I have. So, if there's any alternatives, I'd like to hear them. Thanks!
Try this sample, CheckoutMany method take parameter and return a collection https://github.com/OData/ODataSamples/tree/master/WebApi/v3/ODataActionsSample
Add [EnableQuery] attribute in the CheckOutMany action method, and add $inlinecount queryoption in requset url, you can see odata.count in payload
By the way, V4 support datetime now: http://odata.github.io/WebApi/#04-01-datetime-support
This was recently fixed with the release of version 5.7 (#odata.count is now returned for collections of complex types). More info here:
https://github.com/OData/WebApi/issues/484#issuecomment-153929767

Web API - How to accept comma separated list of parameter values via query string

I have a Web API project I am working on in Visual Studio 2013, and I'd like for my Controllers to accept a comma separated list of values via the query string, similar to this:
http://localhost:12345/api/Procedures/1?embed=doctors,drugs&fields=fieldA,fieldB,fieldC
The reason for this is that I'd like to be able to control if related resources (additional tables) are queried via custom embedding using the embed parameter, and control what fields are returned from the base object using the fields parameter.
I've done some searching on Google but most of what is being suggested relates to extending IModelBinder (http://www.strathweb.com/2013/04/asp-net-web-api-parameter-binding-part-1-understanding-binding-from-uri/) or setting up a custom ActionFilterAttribute (Convert custom action filter for Web API use?) which seems like overkill for something relatively simple.
FYI I am using an Entity Framework dbContext class to connect to my database.
The comma character does not have any specific meaning in HTTP query strings so it does not get treated as a separator by out-of-the-box model binding mechanisms.
As far as I know the approach you mentioned with custom attributes is the simplest you can get. And it does not look like an overkill considering you will only implement the attribute once and use everywhere.

When will Breeze support ANY filtering (where predicate on children relationship)?

We have a need to select records based on the value of a related child table's properties. I discovered today that this feature is supported in odata with a keyword called 'any'. Further it's supported in the default breeze server implemention (using entity framework). Using the same server that my breeze client does, I can enter an odata query in a browser using the 'any' keyword and select records on a related child's field value. For example:
.../Issue?$filter=oIssueImages/any(ii: ii/IssueImageRef eq 4)
And it works! But, there is no support for this in the breezejs client code.
FWIW: I found this breeze feature request: https://breezejs.uservoice.com/forums/173093-breeze-feature-suggestions/suggestions/3988038-adding-any-and-all-filter-operators
Anyone know when breeze will implement this feature?
Updated post: 11/25/13
As of Breeze 1.4.6, 'any' and 'all' operators are now supported.
So your client side Breeze query would look something like:
var query = EntityQuery.from("Issue")
.where("oIssueImages", "any", "IssueImageRef", "==", 4);
myEntityManager.executeQuery(query).then(...)
Also see: http://www.breezejs.com/documentation/query-examples
Older post
Please vote it up. This is a really good feature, but we really do try to accommodate those features that get the most votes.

Using Entity Framework and classic SQL query approaches

I am using ASP.NET MVC with EF 5.0. I am trying to create a dynamic query and execute it using EF.
What I tried
var sConnection = ((SqlConnection)DbContext.Database.Connection);
sConnection.Open();
Thought I will take the EF connection and use that and execute the query and get result in DataTable.
But I saw other option of
DbContext.Database.SqlQuery //But I need to have Entity Type to get the results
DbContext.Database.ExecuteSqlCommand
Is there a way I can execute and get results to DataTable without using my way of getting connection object ?
Any other alternative and best approach wil be useful,
Thanks

Resources