我在here之前问了一个类似的问题,答案解决了我的问题。但是,现在我遇到了与之前相同的问题,但是由于条件的变化,答案不再起作用。
这是我正在运行的查询。我已经检查过someField
被设置为某个东西(它是一个由guid引用其他实体的查找字段)。 result
中没有条目(除非我将条件切换为“不相等”)。这可能是因为该领域没有被引入。
当我断开执行并检查Attributes
时,我看到总共30个中的21个字段(其中一些可能是空的,当然,但是这个一个,即{{ 1}},不是),但我感兴趣的那个,不在那里!
someField
我想念什么,如何解决?
我尝试使用this blog中讨论的QueryExpression query = new QueryExpression
{
EntityName = "entity",
ColumnSet = new ColumnSet{ AllColumns = true },
// Here I tried the code addition #1 below
Criteria =
{
Filters =
{
new FilterExpression
{
Conditions =
{
new ConditionExpression("someField", ConditionOperator.Equal, guid)
}
}
}
}
};
// Here I tried the code addition #2 below
EntityCollection result = Service.RetrieveMultiple(query);
,但实际上并没有成功。我甚至不确定这是否是一种有意义的方法。它看起来如下。
LinkEntities
我也尝试使用solution suggested on MSDN。同样的结果。
LinkEntities =
{
new LinkEntity
{
Columns = new ColumnSet { AllColumns = true },
LinkFromEntityName = "entity",
LinkFromAttributeName = "otherEntityId",
LinkToEntityName = "entity2",
LinkToAttributeName = "entity2Id"
}
},
再一次 - @JamesWood提供的解决方案不再有效,因为我已获得管理员访问权限和所认为的字段不为空。
答案 0 :(得分:2)
Query Expression只为您提供带有值的列,因为空值不会添加到属性字典中,因此不会传输到客户端。这就是为什么在访问属性(或用户GetAttributeValue<T>(String)
)之前总是必须检查Contains的原因。
如果您始终想知道实体上存在哪些字段,则必须使用RetrieveEntityRequest
检索实体的元数据。请参阅MSDN。
答案 1 :(得分:1)
我认为这里的问题出在您的FilterExpression
和/或实际数据中。
从你在这里说的话:
我的结果中没有条目(除非我将条件切换为“不是 等于“)
当你将过滤器设置为“相等”时,没有任何东西等于那个Guid,所以你没有得到任何结果。
当您将过滤器设置为“不等于”时,一切都不等于该条件,因此您开始获得一些结果。当你得到结果时,不包括查找,因为它是记录的空值(因此“等于”过滤器不起作用的原因)。
由于我不确定确切的问题,我建议采取以下步骤:
AllColumns = true
),不用指定FilterExpression
。FilterExpression
。有一个使用查找here进行过滤的示例。
作为旁注;实际上,您不必在要用于过滤的ColumnSet
列中指定。
E.g。在SQL中你可以这样做:
SELECT firstname FROM contact WHERE lastname = 'wood'
答案 2 :(得分:1)
以下是搜索并返回SELECT * FROM entity WHERE someField = "1234"
的第一个实体的方法。
private static Entity GetEntityByGuid(
IOrganizationService service,
String entityLogicalName,
String fieldToConstrainOn,
Guid valuetoConstrainTo)
{
var qe = new QueryExpression(entityLogicalName)
{
ColumnSet = new ColumnSet(true)
};
qe.Criteria.AddCondition(
fieldToConstrainOn, ConditionOperator.Equal, valuetoConstrainTo);
return service.RetrieveMultiple(qe).Entities.FirstOrDefault();
}
您可以像这样调用它(假设new_entity
是您的自定义实体的逻辑名称,而new_somefield
是ID为“ed5081c1-f77b-4f58”的其他实体的外键-a8bf-a8fbe3fe27be“):
var entity = GetEntityByGuid(
service, "new_entity", "new_somefield",
new Guid("ed5081c1-f77b-4f58-a8bf-a8fbe3fe27be"));
并访问new_somefield
的名称:
String name = entity.GetAttributeValue<EntityReference>("new_somefield").Name;