3 c# metadata dynamics-crm-2011
在这篇博客中,它展示了如何检索字段的元数据.
我想知道如何检查(除上述结合try-catch-statement之外)是否存在字段.
原因是当我执行my时QueryExpression,我需要知道要包含哪些列ColumnSet.
现在的Q&D代码是这样的.
private bool DoesFieldExist(String entityName, String fieldName)
{
try
{
RetrieveAttributeRequest req = new RetrieveAttributeRequest();
req.EntityLogicalName = entityName;
req.LogicalName = fieldName;
RetrieveAttributeResponse resp = (RetrieveAttributeResponse)service.Execute(req);
}
catch (Exception) { return false; }
return true;
}
Run Code Online (Sandbox Code Playgroud)
private bool DoesFieldExist(String entityName, String fieldName)
{
RetrieveEntityRequest request = new RetrieveEntityRequest
{
EntityFilters = Microsoft.Xrm.Sdk.Metadata.EntityFilters.Attributes,
LogicalName = entityName
};
RetrieveEntityResponse response
= (RetrieveEntityResponse)service.Execute(request);
return response.EntityMetadata.Attributes.FirstOrDefault(element
=> element.LogicalName == fieldName) != null;
}
Run Code Online (Sandbox Code Playgroud)
该EntityFilters要求添加using Microsoft.Xrm.Sdk.Metadata;,以正常工作,如果你不喜欢在我的例子中,明确提及(因为它是丑陋的,你应该不喜欢它).
我宁愿用FirstOrDefault而不是SingleOrDefault.虽然在这种情况下它不会给你任何问题(属性是否存在),在其他情况下,如果有多个元素满足条件,你可能会得到一个异常(如果你在多个属性上寻找匹配或者是其他可能导致这种情况的东西).
| 归档时间: |
|
| 查看次数: |
4081 次 |
| 最近记录: |