Skip to content
This repository has been archived by the owner on Jun 16, 2024. It is now read-only.

Commit

Permalink
Merge pull request #198 from rbnswartz/rs-order-without-select
Browse files Browse the repository at this point in the history
Fixes Orders in QueryExpression not working if column isn't in column set
  • Loading branch information
jordimontana82 authored Jun 20, 2017
2 parents 7261ac5 + 3f552b5 commit 5418492
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 3 deletions.
6 changes: 3 additions & 3 deletions FakeXrmEasy.Shared/XrmFakedContext.Queries.cs
Original file line number Diff line number Diff line change
Expand Up @@ -324,9 +324,6 @@ public static IQueryable<Entity> TranslateQueryExpressionToLinq(XrmFakedContext
Expression<Func<Entity, bool>> lambda = Expression.Lambda<Func<Entity, bool>>(expTreeBody, entity);
query = query.Where(lambda);

//Project the attributes in the root column set (must be applied after the where clause, not before!!)
query = query.Select(x => x.Clone(x.GetType()).ProjectAttributes(qe, context));

//Sort results
if (qe.Orders != null)
{
Expand Down Expand Up @@ -354,6 +351,9 @@ public static IQueryable<Entity> TranslateQueryExpressionToLinq(XrmFakedContext
}
}

//Project the attributes in the root column set (must be applied after the where and order clauses, not before!!)
query = query.Select(x => x.Clone(x.GetType()).ProjectAttributes(qe, context));

//Apply TopCount

if (qe.PageInfo!=null && qe.PageInfo.Count >0 && qe.PageInfo.PageNumber>0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -591,5 +591,35 @@ public void When_ordering_by_2_columns_simultaneously_right_result_is_returned_d
Assert.True(names[4].Equals("11"));
Assert.True(names[5].Equals("12"));
}

[Fact]
public void When_ordering_column_is_not_in_column_set_ordering_is_still_correct()
{
XrmFakedContext context = new XrmFakedContext();
IOrganizationService service = context.GetOrganizationService();
List<Entity> initialEntities = new List<Entity>();

Entity secondEntity = new Entity("entity");
secondEntity.Id = Guid.NewGuid();
secondEntity["int"] = 2;
secondEntity["text"] = "second";
initialEntities.Add(secondEntity);

Entity firstEntity = new Entity("entity");
firstEntity.Id = Guid.NewGuid();
firstEntity["int"] = 1;
firstEntity["text"] = "first";
initialEntities.Add(firstEntity);

context.Initialize(initialEntities);

QueryExpression query = new QueryExpression("entity");
query.ColumnSet = new ColumnSet("text");
query.AddOrder("int", OrderType.Ascending);

EntityCollection result = service.RetrieveMultiple(query);
Assert.Equal(firstEntity.Id, result.Entities[0].Id);
Assert.Equal(secondEntity.Id, result.Entities[1].Id);
}
}
}

0 comments on commit 5418492

Please sign in to comment.