Subsonic 2 Order By sorting not working - sql-order-by

I am trying to sort (ORDER BY ) a subsonic DAL collection
New templateController.FetchAll().OrderByDesc("Name")
But it is not sorting. Am I missing something?

Used this as a solution
New SubSonic.Select().From(Templatedata.Schema).OrderAsc("Name").ExecuteTypedList(Of Templatedata)()

Related

Doctrine 2 Add custom field using DQL is possible?

I can't solve this problem. I have a very large code for filters using a QueryBuilder instance
$qb = new QueryBuilder();
$qb->select('item)->from->("BundleExample:Item");
$qb->andWhere("item.idProvince = {$idProvinde");
if($price)
$qb->andWhere("price betwenn..");
Ok , there is too much lines.
Now i need to add a virtual column (distance) or overwrite a current field value.
$qb->select('item, COS(..) as distance')
As i understand , i need to create a ResultSetMapping , but i have to re-do all the filtering process and this is very annoying.
Any ideas?
thanks
You don't have to use native queries nor RSM, as Doctrine is capable of returning mixed/hybrid result sets: Pure and Mixed Results.

Bind Distinct LINQ query to Telerik grid

I'm trying to bind a LINQ query that has a Distinct() in it and bind it to a Telerik Grid (this is the Telerik grid still, haven't converted to Kendo yet). I've tried adding AsQueryable() to it also, but still getting a "Specified method is not supported exception."
My query looks like this:
var rewardTypes = (from type in _rewardTypeRepository.GetAll()
from vo in type.ValidOutlets
join ot in _outletRepository.GetAll()
on vo.Outlet.Id equals ot.Id
where userOutletIds.Contains(ot.Id)
select vo.RewardType).Distinct().AsQueryable();
Any ideas or guidance on this would be greatly appreciated.
Thank you,
Matt
Using .Distinct() or IQueryable<T> just lazy loads the collection of data, meaning it's not actually enumerated yet.
Try converting it to a List<T>.
var rewardTypes = (from type in _rewardTypeRepository.GetAll()
from vo in type.ValidOutlets
join ot in _outletRepository.GetAll()
on vo.Outlet.Id equals ot.Id
where userOutletIds.Contains(ot.Id)
select vo.RewardType).ToList();
If you want to remain "light" and not enumerate every item in a potentially large collection, I suggest using the .Skip() and .Take() methods to paginate through results at your convenience. Both those methods work against an IQueryable<T> collection.

How to combine 2different IQueryable/List/Collection with same base class? LINQ Union and Covariance issues

I am trying to combine (union or concat) two lists/collection into one. The two lists have a common base class. e.g. I've tried this:
IQueryable<ContractItem> contractItems = myRepository.RetrieveContractItems();
IQueryable<ChangeOrderItem> changeOrderItems = myRepository.RetrieveChangeOrderItems();
IQueryable<ItemBase> folderItems = contractItems.Concat<ItemBase>(changeOrderItems);
But am getting the LINQ error
DbUnionAllExpression requires arguments with compatible collection ResultTypes.
Anybody know how to do this properly? The only thing I could google was another StackOverflow question:
LINQ Union objects with same Base Class
Thanks.
Use the Cast operator:
IQueryable<ItemBase> folderItems = contractItems
.Cast<ItemBase>()
.Concat(changeOrderItems.Cast<ItemBase>());
The answer to the other question works for LINQ to Objects, but not necessarily for LINQ to Entities or LINQ to SQL.
Alternatively, you can convert to LINQ to Objects by calling AsEnumerable:
IQueryable<ItemBase> folderItems = contractItems
.AsEnumerable()
.Concat<ItemBase>(changeOrderItems);
However, take care in LINQ to Objects; Concat would work without any overhead (iterating through both collections from the database), but Union would pull one of the collections entirely from the database and then iterate through the other.

Linq-to-NHibernate OrderBy Not Working

I'm trying order a Linq to NHibernate query by the sum of it's
children.
session.Linq<Parent>().OrderBy( p => p.Children.Sum( c => c.SomeNumber ) ).ToList()
This does not seem to work. When looking at NHProf, I can see that it
is ordering by Parent.Id. I figured that maybe it was returning the
results and ordering them outside of SQL, but if I add a .Skip
( 1 ).Take( 1 ) to the Linq query, it still orders by Parent.Id.
I tried doing this with an in memory List and it works just
fine.
Am I doing something wrong, or is this an issue with Linq to
NHibernate?
I'm sure I could always return the list, then do the operations on
them, but that is not an ideal workaround, because I don't want to
return all the records.
To order a query by an aggregate value, you need to use a group by query. In your example you need to use a 'group by' with a join.
The equivelant SQL would be something like:
select id, sum(child.parentid) as childsum from dbo.Parent
inner join child on
parent.id= child.parentid
group by id
order by childsum desc
If only Linq2NH could do this for us... but it can't sadly. You can do it in HQL as long as you're ok with getting the id, and the sum back, but not the whole object.
I battled with Linq2NH for months before I abandoned it. Its slow, doesn't support 2nd level cache and only supports VERY basic queries. (at least when I abandoned it 6 months ago - it may have come along leaps and bounds!) Its fine if you are doing a simple home-made application. If your application is even remotely complex ditch it and spend a bit of time getting to know HQL: a little harder to understand but much more powerful.

Outer Joins with Subsonic 3.0

Does anyone know of a way to do a left outer join with SubSonic 3.0 or another way to approach this problem? What I am trying to accomplish is that I have one table for departments and another table for divisions. A department can have multiple divisions. I need to display a list of departments with the divisions it contains. Getting back a collection of departments which each contain a collection of divisions would be ideal, but I would take a flattened result table too.
Using the LINQ syntax seems to be broken (I am new to LINQ though and may be using it wrong), for example this throws an ArgumentException error:
var allDepartments = from div in Division.All()
join dept in Department.All() on div.DepartmentId equals dept.Id into divdept
select divdept;
So I figured I could fall back to using the SubSonic query syntax. This code however generates an INNER JOIN instead of an OUTER JOIN:
List<Department> allDepartments = new Select()
.From<Department>()
.LeftOuterJoin<Division>(DepartmentsTable.IdColumn, DivisionsTable.DepartmentIdColumn)
.ExecuteTypedList<Department>();
Any help would be appreciated. I am not having much luck with SubSonic 3. I really enjoyed using SubSonic 2 and may go back to that if I can't figure out something as basic as a left join.
Getting back a collection of departments which each contain a collection of divisions would be ideal
SubSonic does this for you (if you setup your relationships correctly in the database), just select all Departments:
var depts = Model.Department.All();
There will be a property in each item of depts named Divisions, which contains a collection of Division objects.

Resources