How do I write a breeze Predicate equivalent to the following SQL [closed]

微笑、不失礼 提交于 2020-02-05 07:05:13

问题


SELECT 
    * 
FROM
    Table
WHERE 
  FirstName = 'Wilson' 
  AND (START_DATE = '1/1/2014' OR START_DATE > '1/2/2014')

回答1:


Assume you have controller and it has:

     [HttpGet]
     public IQueryable<Tables> GetTables()
     {
         return _contextProvider.Context.Set<Tables>();
     }

Create Predicate:

var p1 = new breeze.Predicate.create("FirstName ", "==", "Wilson");
var p2 = new breeze.Predicate.create("START_DATE ", "==", "1/1/2014");
var p3 = new breeze.Predicate.create("START_DATE ", ">", "1/1/2014");
var orPred = new breeze.Predicate.or(p2, p3);
var andPred = Predicate.and(p1, orPred );
// You should parse "1/1/2014" to DateTime type first

The query will be:

var query= new breeze.EntityQuery().from("GetTables").where(andPred);



回答2:


@freelancer's answer is headed in the right direction ... but a bit verbose for my tastes.

I'm also not convinced that I can count on an automatic conversion of '1/1/2014' into a DateTime value when the query is analyzed. When I try a predicate like this:

var p2 = breeze.Predicate.create('StartDate', '>', '1/2/2014')

Breeze throws an error in my queries.

Error: '1/2/2014' is not a valid dateTime

Here's an alternative:

var pred = breeze.Predicate
            // weird: any day in 2014 except Jan 2nd ???
            .create('StartDate', '==', new Date('1/1/2014'))
            .or(    'StartDate', '>',  new Date('1/2/2014'))
            .and(   'FirstName', '==', 'Wilson');

var query= breeze.EntityQuery.from("GetTables").where(pred);

Notice the left-to-right composition of the predicates.

  1. The create call predicate creates the date-equality predicate

  2. The or call returns a predicate which is the OR of the first predicate and the second date condition. This is the OR predicate

  3. The third and call returns the AND of the OR-predicate and the name-test.

Here's a nifty trick to display your predicate as an OData query clause:

// Should use the target EntityType but this dummy will do for now
var dummyEntityType = new EntityType(new breeze.MetadataStore());
console.log("OData predicate: " + pred.toODataFragment(dummyEntityType )));

It prints

OData predicate: ((StartDate eq datetime'2014-01-01T08:00:00.000Z') or (StartDate gt datetime'2014-01-02T08:00:00.000Z')) and (FirstName eq 'Wilson')

Dates are tricky!

You didn't ask but I'll give you my $0.02 on date risk.

First, I'd want to be certain that my dates in the database have no time components ... or this query won't work at all because no 'StartDate in your database will pass the equality test.

Second, using text dates like this runs the risk of internationalization problems. Is the '1/2/2014' supposed to be January 2nd or February 1st? I'd eliminate ambiguity by supplying real date values as in this next example.

var pred = breeze.Predicate
            .create('StartDate', '==', new Date(2014, 0, 1)) // Jan===0 in JavaScript
            .or(    'StartDate', '>',  new Date(2014, 0, 2))
            .and(   'FirstName', '==', 'Wilson');

This predicate (like the one above) produces

OData predicate: ((StartDate eq datetime'2014-01-01T08:00:00.000Z') or (StartDate gt datetime'2014-01-02T08:00:00.000Z')) and (FirstName eq 'Wilson')

Third, now you have a potential timezone problem. I'd want to be certain that I eliminate TIMEZONE effects by doing everything in UTC on both client and server. So I'd actually write:

var pred = breeze.Predicate
            .create('StartDate', '==', new Date(Date.UTC(2014, 0, 1))) // Jan===0 in JavaScript
            .or(    'StartDate', '>',  new Date(Date.UTC(2014, 0, 2)))
            .and(   'FirstName', '==', 'Wilson');

This predicate produces:

OData predicate: ((StartDate eq datetime'2014-01-01T00:00:00.000Z') or (StartDate gt datetime'2014-01-02T00:00:00.000Z')) and (FirstName eq 'Wilson')



来源:https://stackoverflow.com/questions/24053168/how-do-i-write-a-breeze-predicate-equivalent-to-the-following-sql

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!