0

Hi can I get help from an expert in ActiveRecord?

I am looking for a solution for a HasAndBelongsToMany relation. I currently have the following

    [HasAndBelongsToMany(Table = "SupplierRecordUrls",ColumnKey = "SupplierID", ColumnRef = "RecordUrlID", Lazy = true, OrderBy="??")]
    public virtual IList<RecordUrl> RecordUrls
    {
        get;
        set;
    }

How do I order the above relation by a column in "RecordUrl", for example ... "Name Asc". I have tried referencing a column directly in the class RecordUrl but I receive a "cannot find column "xxxxxx" error.

Any help is much appreciated.

Samuel Liew
  • 76,741
  • 107
  • 159
  • 260

1 Answers1

0

You can find a OrderBy in a HABTM relation here.

Add Sort to your relation definition, create a Comparer that implements IComparer and that will do the trick.

In your code, that will give somthing like this :

[HasAndBelongsToMany(Table = "SupplierRecordUrls",ColumnKey = "SupplierID", ColumnRef = "RecordUrlID", Lazy = true, Sort="MyProject.RecordUrlNameComparer, MyProject.RecordUrl")]
public virtual IList<RecordUrl> RecordUrls
{
    get;
    set;
}

With :

public class RecordUrlNameComparer: IComparer<RecordUrl>
{
    Int32 System.Collections.Generic.IComparer<RecordUrl>.Compare( RecordUrl x, RecordUrl y )
    {
        return new System.Collections.Comparer( System.Globalization.CultureInfo.CurrentCulture ).Compare( x.Name, y.Name );
    } 
} // public class RecordUrlNameComparer: IComparer<RecordUrl> 

I let you read the article :)

Edit :

Your error is now : Could not instantiate comparator class [MyProject.RecordUrl.RecordUrlComparer, MyProject.RecordUrl] for collection MyProject.Supplier.RecordUrls

What I would do :

  • Create the Comparer as an inner class of RecordUrls.

  • Change the Sort to : Sort = "MyProject.Supplier.RecordUrl$RecordUrlComparer, MyProject.Supplier.RecordUrl"

If it still doesn't work, you still get the "couldn't instantiate" error, try making the Comparer class static. I'm not sure how it is used, but if it's a static call, that may do the trick :)

LaGrandMere
  • 10,265
  • 1
  • 33
  • 41
  • Hey guys thanks for the responses! I have setup the IComparer as you have instructed, and trying to compare URL's in ascending order for example return new System.Collections.Comparer( System.Globalization.CultureInfo.CurrentCulture ).Compare( x.Url, y.Url ); I have also added Sort="MyProject.RecordUrlNameComparer, MyProject.RecordUrl" to the HABTM attribute but it seems to ignore the sort parameter any suggestions? – Sukhbir Saini Dec 14 '10 at 02:01
  • Sort != OrderBy. Sort happens client-side, OrderBy happens server-side. – Mauricio Scheffer Dec 14 '10 at 05:02
  • I am not using OrderBy I am using Sort, but I am receiving the following error: Could not instantiate comparator class [MyProject.RecordUrl.RecordUrlComparer, MyProject.RecordUrl] for collection MyProject.Supplier.RecordUrls - Any ideas ?? – Sukhbir Saini Dec 14 '10 at 05:38
  • @Sukhbir Saini : Edited my post to help you :) – LaGrandMere Dec 14 '10 at 08:21