OData WebAPI: How to hardcode OrderBy in Function? - asp.net

I am using OData WebAPI (Microsoft.AspNet.OData). for some reasons, I need hardcore OrderBy in a function. for example and return like the following,
return DbContext.Companies.OrderByDescending(a => a.CompanyName);
When I executed the function, orderby hardcoded did not work, I also checked sqlprofile, it seem framework did something and remove it. If I use $orderby queryoption, it is no problem.
Is there anyone knows how to solve it? thks in advance.

One workaround to implement is by making CompanyName a part of the composite key and specifying the Column attribute. I doubt that it will work without making the property part of the key and just using the Column attribute but you can quickly try that out.
One other workaround is to insert an orderby clause in ODataQueryOptions in your controller.

Related

How to get distinct paged list from SharpRepository FindAll method with selector?

I was trying to get a field from an entitytype which has many dups using the sharprepository FindAll method with paging. I don't know how to supply the Distinct parameter or if it is even possible?
repo.FindAll(spec, c => c.Field, new PagingOptions<EntityType>(1, 20, "Field", false);
At this point it's not something you can do, though it makes sense that we should add it to the list of things to add. If you wouldn't mind adding that as an issue to GitHub I'm sure we can add it pretty easily. We would just need to figure out the best way to include it.

How can I retrieve field in Propel?

I need to get the whole field 'username' from my DB.
Can I do it with model class or Peer-class?
There are many ways to solve that using Query-class, but command propel:build-model didn't create Query-classes. I don't understand why.
Problem is solved. I use
$users = wiEdmsUserPeer::doSelect(new Criteria())
to get all elements.
Another way is:
$criteria = new Criteria();
$criteria->addSelectColumn($column_name);
wiEdmsUserPeer::doSelect($criteria);
Query classes weren't generated because you probably aren't using the latest version of Propel. Using Query classes is cleaner and works better than criteria, consider upgrading!

How to persist a change in a DBML

I have a table of users called Users
And a view called UsersActive that filters out deactivated users.
When I create my DBML, I drag the Users table in, then I change the property on the table to point to UsersActive.
This works well, until the DBML gets re-created.
Does anyone know how to fix this?
I've tried overriding the
[Table(Name="dbo.Users")]
attribute in a partial class but get the error:
Duplicate 'Table' attribute
Does anyone know how to go about this?
Thanks in advance!
-Ev
You should just be able to add the View to the DBML, just like a table...yes?
Update: No, it will probably not maintain the relationships -- views don't have relationships.
It sounds like your goal is to query active users in a simple way, without having to specify the criterion in each query?
What you might do then is to have a repository class with a method of GetUsers(). That method does the Linq query and ensures that the active criterion is always there.
Perhaps the method would have a signature of Respository.GetUsers(bool includeDeativated = false). Calling GetUsers() without arguments will not return deactivated, but you can override it if desired.

How can I to order an EntityQuery query in a seam app?

My project was originally generated by seam-gen and the action "List" bean, OfficeViewList looks pretty much like it did when first generated.
The bean extends EntityQuery.
Now I want to order the results. What is the best way to do this?
Do I want to add some kind of "order by" class to my EJBQL? Or do I want to set the select order via
Here is the code that seam-gen generated (I have changed the RESTRICTIONS, but otherwise it's the same):
private static final String EJBQL =
"select officeView from OfficeView officeView";
private static final String[] RESTRICTIONS = {
"lower(officeView.addr1) like concat(lower(#{officeViewList.officeView.addr1}),'%')",
"lower(officeView.buildingId) like
concat(lower({officeViewList.officeView.buildingId}),'%')",
"lower(officeView.circuitId) like
concat('%',lower({officeViewList.officeView.circuitId}),'%')",};
public OfficeViewList() {
setEjbql(EJBQL);
setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
setMaxResults(25);
}
The SQL translation is roughly
select * from office_view where order by office_id
I was thinking to use setOrder or setOrderColumn, like this
public OfficeViewList() {
setEjbql(EJBQL);
setRestrictionExpressionStrings(Arrays.asList(RESTRICTIONS));
setOrderColumn("office_id");
setMaxResults(25);
}
but I can't quite figure out how to do it or whether either of these are appropriate. I can't find any documentation that really explains how to use these.
Or do I add some kind of "order by" clause to by EJBQL statement?
Or is there an annotation to add to my entity bean? or to the constructor?
Too many choices, not enough knowledge.
Thank you in advance.
TDR
I ended up adding
setOrderColumn("officeView.officeId");
to the constructor and that did exactly what I wanted.
EntityQuery load the restrictions array only the first time, then he doesn't call restrictions anymore.
So if you want to use the same EntityQuery object and change the restrictions use your second solution, I use it too. Or initialize another EntityQuery object and and set differents restrictions.

Why does Flex's ArrayCollection's Contain method look at memory reference?

When using .contains() on an ArrayCollection in Flex, it will always look at the memory reference. It does not appear to look at an .equals() method or .toString() method or anything overridable. Instead, I need to loop through the ArrayCollection every time and check each individual item until I find what I'm looking for.
Does anyone know why Flex/ActionScript was made this way? Why not provide a way from people to use the contains() method the way they want?
Couldn't you just extend ArrayCollection and override the contains() method? Alternatively you can paste the source for ArrayCollection into an "mx/collections" package in your project and modify the source; this "monkey-patching technique" will override the behavior throughout your entire project. However I would be extremely cautious about changing ArrayCollection in that manner: since it's used all over the place in the Flex APIs there is a good chance you'll start breaking other components in the framework.
The contains() method searches by reference, correct (I believe even for primitives), so if you're trying to find a string or an int in an ArrayCollection, you'll have to do the searching yourself, by some variation of looping or searching. I don't think any of us could tell you why there isn't, say, an optional parameter on that method indicating whether to search by ref or by val, though; so it goes, as they say.
But I'd definitely warn you off monkey-patching the framework code -- that's just asking for trouble. :)
Well, it seems like the ArrayCollection doesn't actually look directly at memory, but only as a last resort. It will attempt to find a Unique ID (UID) for the object. If the UID doesn't exist, it will create one for it using the UIDUtil.as.
You can get around this whole default UID stuff by having your object implement the IUID interface and providing your own UID for the object. The ArrayCollection will look at the UID you provide it.
I would suggest a simple:
in_array($haystack, $arrayCollection->toArray());

Resources