Multiple Sort fields in c#

2,071 views
Skip to first unread message

Robin Minnaard

unread,
Oct 31, 2013, 4:13:37 PM10/31/13
to mongodb...@googlegroups.com

Hope someone can help me,


Via this call i retrieve the OrderBy parameter.

string sortField = docQueryHelp.GetSortField(filter.OrderBy);
bool ascending = filter.IsAsc ?? false;

In “filter” the OrderBy value is “EndTime” (field in MongoDB).

This is set via a php script that talks to the windows server.

$live_specification_filter->OrderBy = ‘EndTime’;
$live_specification_filter->IsAsc = true;

Now i tried to have two fields to sort on by passing this in the php script:

$live_specification_filter->OrderBy = ‘EndTime, Specification.Category’;
$live_specification_filter->IsAsc = true;

So first sort on EndTime then Specification.Category.

Using only 1 OrderBy value works, but how can set/format the above OrderBy
in the .Net code?

string sortField = docQueryHelp.GetSortField(filter.OrderBy);

Would be great if you could help me.

Robert Stam

unread,
Oct 31, 2013, 4:19:55 PM10/31/13
to mongodb...@googlegroups.com
You can write something like this:

var query = Query.Null; // replace with some meaningful query
var order = SortBy.Ascending("EndTime").Ascending("Specification.Category");
var cursor = collection.Find(query).SetSortOrder(order);



--
You received this message because you are subscribed to the Google Groups "mongodb-csharp" group.
To unsubscribe from this group and stop receiving emails from it, send an email to mongodb-cshar...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Robin Minnaard

unread,
Oct 31, 2013, 4:39:24 PM10/31/13
to mongodb...@googlegroups.com
Thank you Robert,

I have this code:

            AuctionQueryDocHelper docQueryHelp = new AuctionQueryDocHelper();
            Document where = docQueryHelp.GetQueryCondition(filter);
            
            string sortField = docQueryHelp.GetSortField(filter.OrderBy);
            bool ascending = filter.IsAsc ?? false;

            GroupResult[] grpResults = null;

            using (MyMongoDb mm = new MyMongoDb())
            {
                var coll = mm.GetCollection<Auction>("Auction") as MongoDB.MongoCollection<Auction>;
                if (filter.GroupByOffer.GetValueOrDefault())
                {
                    grpResults = docQueryHelp.GetHighestAuctionIDsGroupByProductID(where, coll);
                    var ids = grpResults.Select(a => a.ID).ToArray();
                    where = new Document("_id", Op.In(ids));
                }


                var documents = coll
                        .Find(where, count, start)
                        .Sort(sortField, ascending ? IndexOrder.Ascending : IndexOrder.Descending);

                var results = documents.Documents.ToArray();
                docQueryHelp.FillAuctionGroupProperty(results, grpResults);

                return results;

            }

How should i fit this in the green part of the code?

Thanx

Op donderdag 31 oktober 2013 21:19:55 UTC+1 schreef Robert Stam:

Robert Stam

unread,
Nov 1, 2013, 10:00:08 AM11/1/13
to mongodb...@googlegroups.com
Are you sure you are using the latest version of the C# driver?

The driver binaries are available on NuGet at: 
http://www.nuget.org/List/Packages/mongocsharpdriver

The driver binaries are also available at:
https://github.com/mongodb/mongo-csharp-driver/releases/download/v1.8.3.9/CSharpDriver-1.8.3.zip
https://github.com/mongodb/mongo-csharp-driver/releases/download/v1.8.3.9/CSharpDriver-1.8.3.msi

The following source code your provided doesn't match the C# driver API:

var documents = coll
    .Find(where, count, start)
    .Sort(sortField, ascending ? IndexOrder.Ascending : IndexOrder.Descending);
var results = documents.Documents.ToArray();

MongoCollection does not have a Find method that takes three parameters. MongoCursor does not have a Sort method or a Documents property.

This code should look something like this:

var cursor = collection.Find(where)
    .SetLimit(count)
    .SetSkip(start)
    .SetSortOrder(ascending ? SortBy.Ascending(sortField) : SortBy.Descending(sortField));
var results = cursor.ToArray();



Reply all
Reply to author
Forward
0 new messages