Orderby not working properly

31 views
Skip to first unread message

KoteswaraRao Ankalla

unread,
Apr 25, 2012, 4:19:11 AM4/25/12
to ravendb
Hi All,

We observed some issues with orderby in raven db, when we are trying
to get results using different sort conditions using the below code:

Here is the Method to get all Projects/documents in given sort order.

public static List<CommunityProject> GetAllProjects(string sortBy)
{
using (var documentSession = Instance.OpenSession())
{
var linqQuery = documentSession.Query<CommunityProject,
ProjectListIndex>();

switch (sortBy)
{
case "recent":
linqQuery = query.OrderByDescending(x => x.CreationDateUtc);
case "view":
linqQuery = query.OrderByDescending(x => x.NumberOfViews);
case "photo":
linqQuery = query.OrderByDescending(x => x.PrjImages.Count);
case "alpha":
linqQuery = query.OrderBy(x => x.Name);
default:
linqQuery = query.OrderByDescending(x => x.CreationDateUtc);

}

return linqQuery.ToList();

}
}


Here is the Index:

public class ProjectListIndex :
AbstractIndexCreationTask<CommunityProject>
{
public ProjectListIndex()
{
Map = docs => from doc in docs
select new
{

PrjType =
doc.PrjType.Select(x => x),
IsEditorial =
doc.IsEditorial,
PrjLocation_City =
doc.PrjLocation.City,
PrjLocation_StateCode =
doc.PrjLocation.StateCode,
PrjLocation_State =
doc.PrjLocation.State,
PrjLocation_CountryCode =
doc.PrjLocation.CountryCode,
FirmName = doc.FirmName,
PrincipalInCharge =
doc.PrincipalInCharge,
Client = doc.Client,
IsActivePrj =
doc.IsActivePrj,
PrjDetail_Status =
doc.PrjDetail.Status,
PrjDetail_YearCompleted =
doc.PrjDetail.YearCompleted,
PrjDetail_Budget = (decimal)
doc.PrjDetail.Budget,
PrjDetail_PrjSize_Size =
doc.PrjDetail.PrjSize.Size,
PrjDetail_GreenCertifications
=

doc.PrjDetail.GreenCertifications.Select(x => x),
PrjDetail_AwardsWon_Title =
doc.PrjDetail.AwardsWon.Select(award =>
award.Title),
PrjDetail_Tags =
doc.PrjDetail.Tags.Select(x => x),
CreationDateUtc =
doc.CreationDateUtc,
NumberOfViews =
doc.NumberOfViews,
PrjImages_Count =
doc.PrjImages.Count,
UserName = doc.UserName,
Name = doc.Name,
Query = new object[]
{
doc.Name,

doc.Description,

doc.PrincipalInCharge,
doc.FirmName,
doc.UserName,

doc.PrjLocation.Country,

doc.PrjLocation.CountryCode,

doc.PrjLocation.City,

doc.PrjLocation.State,

doc.PrjLocation.StateCode,

doc.PrjDetail.Status,

doc.PrjType.Select(x => x),

doc.PrjProducts.Select(x => x.Name),

doc.PrjImages.Select(x => x.ImgDescription),

doc.PrjImages.Select(x => x.ImgName),

doc.PrjProducts.Select(x => x.Manufacturer),

doc.PrjDetail.AwardsWon.Select(award => award.Title),

doc.PrjDetail.GreenCertifications.Select(x => x),

doc.PrjDetail.Tags.Select(x => x)
}

};

}
}



The issue is :
Raven is not returning results in specified order, for example when we
query for project docs which contains most images first, it is not
returning results in correct order and not sure which order it is
following.

It is also not working for NumberOfViews.

For rest of the above cases(CreationDateUtc, Name) it is working fine


Please let us know, if you have any ideas to resolve our issues.

Thanks,
Koti.

Mauro Servienti

unread,
Apr 25, 2012, 4:58:06 AM4/25/12
to rav...@googlegroups.com
What is query and linqQuery, that code should not compile. You create a linqQuery and then use a query to build order

.m

Matt Warren

unread,
Apr 25, 2012, 5:02:03 AM4/25/12
to rav...@googlegroups.com
You need to specify the sort order for any fields that don't sort lexiographicially, for instance the PrjDetail_Count filed because it is a number.

Ram Kumar

unread,
Apr 25, 2012, 5:01:25 AM4/25/12
to ravendb
just a mistake here is the corrected version

public static List<CommunityProject> GetAllProjects(string sortBy)
{
using (var documentSession = Instance.OpenSession())
{
var linqQuery =
documentSession.Query<CommunityProject,
ProjectListIndex>();

switch (sortBy)
{
case "recent":
linqQuery =
linqQuery .OrderByDescending(x => x.CreationDateUtc);
case "view":
linqQuery =
linqQuery .OrderByDescending(x => x.NumberOfViews);
case "photo":
linqQuery =
linqQuery .OrderByDescending(x => x.PrjImages.Count);
case "alpha":
linqQuery = linqQuery .OrderBy(x =>
x.Name);
default:
linqQuery =
linqQuery .OrderByDescending(x => x.CreationDateUtc);

}

return linqQuery.ToList();

}

ram.

KoteswaraRao Ankalla

unread,
Apr 25, 2012, 5:28:28 AM4/25/12
to rav...@googlegroups.com
Thank you...
We will try and let you know how it goes..

--Koti.

--

Thanks & Regards
KoteswaraRao A

KoteswaraRao Ankalla

unread,
Apr 25, 2012, 5:55:26 AM4/25/12
to rav...@googlegroups.com, matt...@gmail.com
Hi Matt and All,

Thank you very much for your response to resolve sorting issues.
It is working fine for us now..

Thanks,
Koti.

Reply all
Reply to author
Forward
0 new messages