字典中不存在给定的键 - MS CRM FetchXML

The given key was not present in the dictionary - MS CRM FetchXML

大量编辑:

似乎是通过让事情变得简单来说明问题,我把事情变得更复杂了。

这是完整的功能:

[AllowAnonymous]
[HttpGet]
public HttpResponseMessage getCPDActiviesByMemberNumber(int memberNumber) {

List<Entity> sourceData = null;
List<CPDActivity> membersActivites = new List<CPDActivity>();
List<member> memberRecords = new List<member>();

try {

    string xmlFile = "";
    string memberNumberString = memberNumber.ToString();
    xmlFile = File.ReadAllText(AppDomain.CurrentDomain.BaseDirectory + @"_Code\Fetch\MembersActivities.xml");
    xmlFile = String.Format(xmlFile, memberNumberString);
    OrganizationServiceClient crm = new OrganizationServiceClient();
    sourceData = crm.GetEntities(xmlFile);

    if (sourceData.Count > 0) {

        member member = new member();

        foreach (Entity entity in sourceData) {

            CPDActivity cpdActivity = new CPDActivity();

            // sb_membership
            member.ID = entity.GetAttributeValue<Guid>("sb_membershipid");
            member.MemberNumber = entity.GetAttributeValue<String>("sb_membershipno");
            member.ContactID = entity.GetAttributeValue<EntityReference>("sb_contactid").Id.ToString(); ;
            member.MembershipStatus = entity.FormattedValues["statuscode"].ToString();
            member.MemberGrade = entity.GetAttributeValue<String>("sb_name");
            member.StartDate = entity.GetAttributeValue<DateTime>("sb_startdate").ToString();
            member.ExpiryDate = entity.GetAttributeValue<DateTime>("sb_expirydate").ToString();
            // contact (membercontact)
            member.Title = entity.FormattedValues["membercontact.sb_title"].ToString();
            member.FirstName = entity.GetAttributeValue<AliasedValue>("membercontact.firstname").Value.ToString();
            member.LastName = entity.GetAttributeValue<AliasedValue>("membercontact.lastname").Value.ToString();
            member.RegionCode = entity.GetAttributeValue<AliasedValue>("membercontact.sb_regioncode").Value.ToString();
            member.Email = entity.GetAttributeValue<AliasedValue>("membercontact.emailaddress1").Value.ToString();
            // contact (membercontact) - sb_cpdactivity (cpdactivity) 
            member.Region = entity.GetAttributeValue<AliasedValue>("region.sb_name").Value.ToString();
            // sb_cpdactivity (cpdactivity)
            cpdActivity.ActivityName = entity.GetAttributeValue<AliasedValue>("cpdactivity.sb_name").Value.ToString();
            cpdActivity.CreatedOn = (DateTime)entity.GetAttributeValue<AliasedValue>("cpdactivity.createdon").Value;
            cpdActivity.CpdHours = Convert.ToSingle(entity.GetAttributeValue<AliasedValue>("cpdactivity.sb_cpdhours").Value);
            cpdActivity.ActivityDate = (DateTime)entity.GetAttributeValue<AliasedValue>("cpdactivity.sb_activitydate").Value;
            cpdActivity.StatusCode = entity.FormattedValues["cpdactivity.statuscode"].ToString();
            cpdActivity.CpdActivityID = (Guid)entity.GetAttributeValue<AliasedValue>("cpdactivity.sb_cpdactivityid").Value;
            cpdActivity.MemberContactID = ((EntityReference)entity.GetAttributeValue<AliasedValue>("cpdactivity.sb_membercontactid").Value).Id;
            //
            cpdActivity.EventType = entity.GetAttributeValue<AliasedValue>("cpdactivity.sb_type").Value.ToString();  // <<<<< problem here
            cpdActivity.EventType = entity.FormattedValues["cpdactivity.sb_type"].ToString();  // <<<<< problem here
            //
            member.Activities.Add(cpdActivity);
            memberRecords.Add(member);
        }

        return Request.CreateResponse(HttpStatusCode.OK, memberRecords);

    } else {

        return Request.CreateResponse(HttpStatusCode.OK, "getCPDActiviesByMemberId " + "no records found");

    }

} catch (Exception ex) {

    string error = ex.Message.ToString();
    string errors = error;
    return Request.CreateResponse(HttpStatusCode.OK, "getMembers - error : " + error);

}

}

这是 FetchXML:

<fetch version="1.0" output-format="xml-platform" mapping="logical" distinct="false">
  <entity name="sb_membership">
    <attribute name="sb_contactid" />
    <attribute name="sb_name" />
    <attribute name="sb_membershipno" />
    <attribute name="sb_expirydate" />
    <attribute name="sb_startdate" />
    <attribute name="statuscode" />
    <attribute name="sb_membershipid" />
    <order attribute="sb_name" descending="false" />
    <filter type="and">
      <condition attribute="statecode" operator="eq" value="0" />
      <condition attribute="sb_membershipno" operator="eq" value='{0}' />
    </filter>
    <link-entity name="contact" from="contactid" to="sb_contactid" visible="false" link-type="outer" alias="membercontact">
      <attribute name="sb_title" />
      <attribute name="lastname" />
      <attribute name="firstname" />
      <attribute name="mobilephone" />
      <attribute name="emailaddress1" />
      <attribute name="sb_regioncode" />
      <link-entity name="sb_geographicregion" from="sb_regioncode" to="sb_regioncode" link-type="outer" alias="region">
        <attribute name="sb_name" />
      </link-entity>
    </link-entity>
    <link-entity name="sb_cpdactivity" from="sb_membercontactid" to="sb_contactid" visible="false" link-type="outer" alias="cpdactivity">
      <attribute name="sb_cpdactivityid" />
      <attribute name="sb_membercontactid" />
      <attribute name="sb_name" />
      <attribute name="sb_type" />
      <attribute name="createdon" />
      <attribute name="sb_activitydate" />
      <attribute name="statuscode" />
      <attribute name="sb_cpdhours" />
    </link-entity>
  </entity>
</fetch>

问题是在尝试获取 sb_type(注意不是前面提到的 eventdateid)属性(确实出现在数据库中的列)时出现错误 "The given key was not present in the dictionary." 所以该列确实出现在数据库 table 中,并且在 fetchXML 中被引用,与它在数据库中完全一样,但是在调试时查看实体时,属性没有列出,不像 fetchXML 中的其他属性,例如 'sb_name'.

这是 FetchXML 查询的预期行为。当您的结果集将在所有记录的列(属性)中具有 NULL 值时,该属性将不会包含在 EntityCollection 结果集中。

如果至少有一条记录具有该列的值,则结果集将包含该列。

像这样检查并继续。

var thing;
if(entity.Contains("sb_eventdateid")){
    thing = entity.GetAttributeValue<Guid>("sb_eventdateid");
}

更新: 当您获得 null 列时,最好像下面那样处理 null 检查 - 在访问 .Value

之前
if(entity.Attributes.Contains("cpdactivity.sb_type") && entity.GetAttributeValue<AliasedValue>("cpdactivity.sb_type") != null)
{
            cpdActivity.EventType = entity.GetAttributeValue<AliasedValue>("cpdactivity.sb_type").Value.ToString();  
            cpdActivity.EventType = entity.FormattedValues["cpdactivity.sb_type"].ToString();
}