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

Commit 8baafbf

Browse files
Merge pull request #353 from BetimBeja/LinkedEntityQueryFix
Fix for #324
2 parents 719d3a8 + 7e0d4ed commit 8baafbf

File tree

2 files changed

+70
-1
lines changed

2 files changed

+70
-1
lines changed

FakeXrmEasy.Shared/XrmFakedContext.Queries.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -367,7 +367,7 @@ public static IQueryable<Entity> TranslateLinkedEntityToLinq(XrmFakedContext con
367367
query = query.Join(inner,
368368
outerKey => outerKey.KeySelector(linkFromAlias, context),
369369
innerKey => innerKey.KeySelector(le.LinkToAttributeName, context),
370-
(outerEl, innerEl) => outerEl.JoinAttributes(innerEl, new ColumnSet(true), leAlias, context));
370+
(outerEl, innerEl) => outerEl.Clone(outerEl.GetType()).JoinAttributes(innerEl, new ColumnSet(true), leAlias, context));
371371

372372
break;
373373
case JoinOperator.LeftOuter:

FakeXrmEasy.Tests.Shared/FakeContextTests/RetrieveMultiple/QueryLinkEntityTests.cs

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,5 +1058,74 @@ public void When_There_Are_Multiple_LinkedEntities_With_The_Same_Entitiy_And_One
10581058
Assert.Equal("User2", ((AliasedValue)resultingEntity["systemuserwithalias.fullname"]).Value);
10591059
Assert.Equal("User1", ((AliasedValue)resultingEntity["systemuser2.fullname"]).Value);
10601060
}
1061+
1062+
[Fact]
1063+
public void TestRetriveMultipleWithLinkEntityWithAlternateNullField()
1064+
{
1065+
// ARRANGE
1066+
1067+
List<Entity> initialEntities = new List<Entity>();
1068+
1069+
Entity parentEntity = new Entity("parent");
1070+
parentEntity["parentname"] = "parent name";
1071+
parentEntity.Id = Guid.NewGuid();
1072+
initialEntities.Add(parentEntity);
1073+
1074+
// create the first child which has the "myvalue" field set to "value"
1075+
Entity childEntity1 = new Entity("child");
1076+
childEntity1["parent"] = parentEntity.ToEntityReference();
1077+
childEntity1["name"] = "entity1";
1078+
childEntity1["myvalue"] = "value";
1079+
childEntity1.Id = Guid.NewGuid();
1080+
initialEntities.Add(childEntity1);
1081+
1082+
// create the second child which has the "myvalue" field set to null
1083+
Entity childEntity2 = new Entity("child");
1084+
childEntity2["parent"] = parentEntity.ToEntityReference();
1085+
childEntity2["name"] = "entity2";
1086+
childEntity2["myvalue"] = null;
1087+
childEntity2.Id = Guid.NewGuid();
1088+
initialEntities.Add(childEntity2);
1089+
1090+
XrmFakedContext context = new XrmFakedContext();
1091+
IOrganizationService service = context.GetOrganizationService();
1092+
1093+
context.Initialize(initialEntities);
1094+
1095+
// the query selects the "parent" entity, and joins to the "child" entities
1096+
QueryExpression query = new QueryExpression("parent");
1097+
query.ColumnSet = new ColumnSet("parentname");
1098+
1099+
LinkEntity link = new LinkEntity("parent", "child", "parentid", "parent", JoinOperator.Inner);
1100+
link.EntityAlias = "c";
1101+
link.Columns = new ColumnSet("name", "myvalue");
1102+
1103+
query.LinkEntities.Add(link);
1104+
1105+
// ACT
1106+
1107+
DataCollection<Entity> results = service.RetrieveMultiple(query).Entities;
1108+
1109+
// ASSERT
1110+
1111+
// fields for the first entity work as expected...
1112+
string entity1Name = results[0].GetAttributeValue<AliasedValue>("c.name").Value as string;
1113+
string entity1Value = results[0].GetAttributeValue<AliasedValue>("c.myvalue").Value as string;
1114+
1115+
Assert.Equal("entity1", entity1Name);
1116+
Assert.Equal("value", entity1Value);
1117+
1118+
// fields for the second entity do not.
1119+
// The child "name" field is correct, but the "myvalue" field is returning the value of the previous
1120+
// entity when it should be returning null
1121+
string entity2Name = results[1].GetAttributeValue<AliasedValue>("c.name").Value as string;
1122+
string entity2Value = results[1].GetAttributeValue<AliasedValue>("c.myvalue")?.Value as string;
1123+
1124+
// this works fine:
1125+
Assert.Equal("entity2", entity2Name);
1126+
1127+
// this fails (entity2Value is "value")
1128+
Assert.Equal(null, entity2Value);
1129+
}
10611130
}
10621131
}

0 commit comments

Comments
 (0)