BreezeJS query orderBy length? - breeze

I have a breeze query to fetch some values from my database.
If I want to order the values alphabetically, then the query looks like this:
var query = EntityQuery.from("Words")
.orderBy("Text");
The thing is I want to sort the values returned first by length, then alphabetically.
For example, this set of results is ordered alphabetically:
a
ab
b
bb
How do I make it ordered like this:
a
b
ab
bb
??
I know I can write a function to order the data the way I want to AFTER it's returned from the server, but I'm wondering if it's possible to change the query so the data will be sorted like described above just by changing the query?

You can accomplish this by adding an additional filter step on the server before sending the data back to the client.
[HttpGet]
public IQueryable<Word> Words() {
return ContextProvider.Context.Words.ToList().OrderBy(customOrderByImpl;
}
This may be less than performant because with your example I don't think that you can take advantage of any database sorting ( at least thru EF). This is why the call to ToList occurs before sorting the results in memory on the server.
Hope this makes sense.

Related

breeze - how to disable ko wrapping?

I have a query that is read only and returns a large number of entities.
I do not want change tracking or knockout wrapping for this query.
Is there a way to disable?
If you return results as a projection then Breeze will not try to wrap them. So
var q = EntityQuery.from("Customers");
will return "wrapped" Customer objects. However if you write
var q = EntityQuery.from("Customers").select( "companyName, address, city")
then Breeze will return an array of anon "unwrapped" objects each with 3 properties ( "companyName", "address" and "city").
I can't really think of another approach, if you really want entire entities but do not want to Breeze to "wrap" them. But... this does seem like a reasonable request, so please add a User Voice feature request for an ability to mark a query as "noTracking". We take these requests seriously.

Breeze.js and odata: anything like inlineCount but without filtering?

In Breeze.js inlineCount(http://www.breezejs.com/sites/all/apidocs/classes/EntityQuery.html#method_inlineCount) implements the odata inlinecount parameter (http://www.odata.org/documentation/uri-conventions#InlinecountSystemQueryOption)
so it shows the total count after filtering but without paging.
But is there an "inline" way in either Breeze or odata to also get the full count without filtering in a single breeze query? i.e. the count of all records that would have been returned without the specified filtering. I know I could do a seperate query for this, but was hoping for a way to do it in a single operation.
Apologies if this is a silly question; I'm still getting up to speed with odata and Breeze. The reason I ask is I'm using datatables.net and it expects both the total count, filtered count and number of records so it can display something like
Showing 1 to 10 of 48 entries (filtered from 148 total entries)
I cannot come up with an easy way to do this with a single query.
If I understand your question, you want basically three counts for each query you submit
The count of the query results actually returned - ( easily
obtained via 'results.length' )
The count of the query results that would have been returned
without any skip or take ( obtained via the ".inlinecount()"
method on the query.
The count of the query results that would have been returned
without any filter or skip or take. i.e. the count of the entire
"resource". ( This one is difficult without a separate query).
If this is correct, then I think that you are stuck with performing two queries, one to get the count for the entire "resource" (i.e. #3 above) and another using the "inlinecount" method for #1 and #2

How to order the data back from Amazon simpleDB int specific column order

I'm using Amazon's SimpleDB Java client to read data from SimpleDB. The problem I have is even though I specified the columns in the some order in the SelectRequest like the following:
SelectRequest req = new SelectRequest("SELECT TIMESTAMP, TYPE, APP, http_status, USER_ID from mydata");
SElectResult res = _sdb.select(req);
..
It returned data in following column order:
APP, TIMSTAMP, TYPE, USER_ID, http_status,
It seems it automatically reordered the columns in ascend order. Is there any way I can force the order as I specified in the select clause?
The columns returned are not an ordered list but an unordered set of attributes. You can't control the order they come back in. SELECT is designed to work even in cases where some of the attributes in your query don't exist for every (or any) returned items. In those cases specifically you wouldn't be able to rely on order anyway. I realize that's small consolation if you have structured your data set so that the attributes are always present.
However, since you know the desired order ahead of time, it should be pretty easy to pull the data out of the result in the proper order. It's just XML after all, or in the case of the Java client, freshly parsed XML.
The Select operation returns a set of Attributes for ItemNames that match the select expression.
SimpleDB docs for SELECT

Linq-to-SQL query - Need to filter by IDs returned by Full-Text Search sql functions - Hitting limit for Contains

My objective:
I have built a working controller action in MVC which takes user input for various filter criteria and, using PredicateBuilder (part of LinqKit - sorry, I'm not allowed enough links yet) builds the appropriate LINQ query to return rows from a "master" table in SQL with a couple hundred thousand records. My implementation of the predicates is totally inelegant, as I'm new to a lot of this, and under a very tight deadline, but it did make life easier. The page operates perfectly as-is.
To this, I need to add a Full-Text search filter. Understanding the way LINQ translates Contains to LIKE(%%), using the advice in Simon Blog: LINQ-to-SQL - Enabling Full-Text Searching, I've already prepared Table Functions in SQL to run Freetext queries on the relevant columns. I have 4 functions, to match the query against 4 separate tables.
My approach:
At the moment, I'm building the predicates (I'll spare you) for the initial IQueryable data object, running a LINQ command to return them, like so:
var MyData = DB.Master_Items.Where(outer);
Then, I'm attempting to further filter MyData on the Keys returned by my full-text search functions:
var FTS_Matches_Subtable_1 = (from tbl in DB.Subtable_1
join fts in DB.udf_Subtable_1_FTSearch(KeywordTerms)
on tbl.ID equals fts.ID
select tbl.ForeignKey);
... I have 4 of those sets of matches which I've tried to use to filter my original dataset in several ways with no success. For instance:
MyNewData = MyData.Where(d => FTS_Matches_Subtable_1.Contains(d.Key) ||
FTS_Matches_Subtable_2.Contains(d.Key) ||
FTS_Matches_Subtable_3.Contains(d.Key) ||
FTS_Matches_Subtable_4.Contains(d.Key));
I just get the error: The incoming tabular data stream (TDS) remote procedure call (RPC) protocol stream is incorrect. Too many parameters were provided in this RPC request. The maximum is 2100.
I get that it's because I'm trying to pass a relatively large set of data into the Contains function and LINQ is converting each record into a separate parameter, exceeding the limit.
I just don't know how to get around it.
I found another post linq expression to return property value which seemed SO promising. I tried ifwdev's solution (2nd highest ranked answer): using LinqKit to build an extension that will break up the queries into manageable chunks. But I can't figure out how to implement it. Out of my depth right now maybe?
Is there another approach that I'm missing? Some simpler way to accomplish this that I've overlooked?
Sorry for the long post. But thank you for any help you can provide!
This is a perfect time to go back to raw ado.net.
Twisting things around just to use linq to sql is probably just as time consuming if you wrote the query and hydration by hand.

Fetch data from multiple tables and sort all by their time

I'm creating a page where I want to make a history page. So I was wondering if there is any way to fetch all rows from multiple tables and then sort by their time? Every table has a field called "created_at".
So is there any way to fetch from all tables and sort without having Rails sorting them form me?
You may get a better answer, but I would presume you would need to
Create a History table with a Created date column, an autogenerated Id column, and any other contents you would like to expose [eg Name, Description]
Modify all tables that generate a "history" item to consume this new table via Foreign Key relationship on History.Id
"Mashing up" tables [ie merging different result sets into a single result set] is a very difficult problem, but you would effectively be doing the above anyway - just in the application layer, so why not do it correctly and more efficiently in the data layer.
Hope this helps :)
You would need to perform the sql like:
Select * from table order by created_at incr
: Store this into an array. Do this for each of the data sources, and then perform a merge sort on all the arrays in Ruby. Of course this will work well for small data sets, but once you get a data set that is large (ie: greater than will fit into memory) then you will have to use a different collect/merge algorithm.
So I guess the answer is that you do need to perform some sort of Ruby, unless you resort to the Union method described in another answer.
Depending on whether these databases are all on the same machine or not:
On same machine: Use OrderBy and UNION statements in your sql to return your result set
On different machines: You'll want to test this for performance, but you could use Linked Servers and UNION, ORDER BY. Alternatively, you could have ruby get the results from each db, and then combine them and sort
EDIT: From your last comment about different tables and not DB's; use something like this:
SELECT Created FROM table1
UNION
SELECT Created FROM table2
ORDER BY created

Resources