LinqExtender 1.3.1

Last week, I have released a patch for LinqExtender project at Codeplex. You can find the full feature list at release page. One issue that I will talk about here is orderby clause and its related in-memory sort.

Service based apps are of fixed set of operations. Now, it might happen that you need the list of most of popular photo tags from Flickr and you need to sort them by title as well. But, unfortunately you come to know that it has no parameter that sorts by title.

Now , those who have played around the toolkit (since there are lots of downloads :-)) , must know that for the following query

var query = from tag in _context.PopularTags
where tag.Period == TagPeriod.Week orderby tag.Title ascending
select tag;

I will have a Bucket.OrderByClause inside Query<T>.Process override. Bucket.OrderByClause has two properties (FieldName, IsAscending)  by which, we can understand the orderby query nature and take action on that. This is good , if external method that we are calling supports the orderby info. If not , then alternatively we can do something that follows

protected override void Process
(LinqExtender.Interface.IModify<T> items, Bucket bucket)
{
    IEnumerable<QueryObject> results = GetResult( ... );    
   // add the result to the extender collection, "true" defines that it should be 
   // be sorted by the orderby specified in query expression.    
    items.AddRange(result, true);         
}
OR
protected override void Process
(LinqExtender.Interface.IModify<T> items, Bucket bucket)
{
    foreach (var item in unfilterItems) 
    { 
       // do some of work your own work
       // finally add it to the collection
       items.Add(item);
    }
    // sort the items if any orderby query is used 
   items.Sort();
}

Either way , the result will be the same. As, it looks clear that it is doing in-memory sort. Therefore, you have to be careful on how much data you are performing this. Generally, this type of sort is best suited for predictable result sets.

You have seen orderby clause in LINQ query here and there.But have you ever thought that someday when you will be writing your custom provider , how will you deal with it? The best case could be using IComparer implementation. Though, you can dig in the code to find the every detail of how I implemented it, in brief here how it works.

In LinqExtender , I have created a class called QueryItemsComparer. As, I already build Bucket.OrderByClause object from query. All I needed to do the sort in base List<T> through this custom comparer. part of the code looks like

this.Sort(new QueryItemComparer<T>
(_queryObject.OrderByClause.FieldName, _queryObject.OrderByClause.IsAscending));

Infact, the secrect behind orderby in terms of in-memory sort is nothing but IComparer :-).

Finally, I found users sending me requests of new stuffs to be implemented in the toolkit, its really nice to hear feedback (both good and bad), keep those coming. In this way, I will be able to make it more useful in coming days.

Project url : www.codeplex.com/linqextender

kick it on DotNetKicks.com

2 Comments

Comments have been disabled for this content.