如何使用queryexpression和silverlight从帐户中检索parentaccountid

时间:2011-08-11 07:25:14

标签: silverlight dynamics-crm-2011

我的查询返回account.name,account.account和account.parentaccountid。

我正在使用Silverlight和CRM2011。

现在我无法找到如何从parentaccountid属性中提取值。

我的VS项目中包含silverlightextensionmethods.cs文件,我正在使用GetAttributeValue<Guid>("parentaccountid")parentaccountid获取值。

返回的值为空。

有没有人想过如何实现这个目标?

我可以获得任何其他属性值,但是帐户中的parentaccountid和联系中的parentcustomerid使我的生活变得非常困难。

代码: 首先我创建QUERYEXPRESSION:


string temp="name;accountid;parentaccountid";
string[] fields = temp.Split(';');
 QueryExpression query = new QueryExpression()
 {
           EntityName = entity,
          ColumnSet = new ColumnSet { Columns = new System.Collections.ObjectModel.ObservableCollection<string>(fields) },

         Criteria = new FilterExpression
          {
              FilterOperator = LogicalOperator.And,
             Conditions = 
             {
                   new ConditionExpression
                   {
                                AttributeName = parentidfield,
                                Operator = ConditionOperator.Equal,
                                Values = { id }
                   }
              }
           }
 };

 OrganizationRequest req = new OrganizationRequest();
 req.RequestName = "RetrieveMultiple";

 req["Query"] = query;

 service.BeginExecute(req, new AsyncCallback(GetChildren_ExecuteCallBack), service);

下一步我打算阅读价值回复

void GetChildren_ExecuteCallBack(IAsyncResult childresult)
        {
            List<TreeRecord> listc = new List<TreeRecord>();
            try
            {
                OrganizationResponse childresponse = ((IOrganizationService)childresult.AsyncState).EndExecute(childresult);
                EntityCollection childresults = (EntityCollection)childresponse["EntityCollection"];

                if (childresults.Entities.Count > 0)
                {
                    TreeConfig sitm = new TreeConfig();
                    string sdisplay = "";
                    string[] fields = "".Split(';');
                    string sid = "";
                    string pid = "";
                    foreach (Entity childentity in childresults.Entities)
                    {
                        foreach (TreeConfig sitem in Configs)
                        {
                            if (sitem.EntityName == childentity.LogicalName)
                            {
                                sitm = sitem;
                            }
                        }

                        TreeRecord childitem = new TreeRecord();
                        string sValue = "";
                        sdisplay = "name;accountid;parentaccountid"; 
                        fields = sdisplay.Split(';');
                        sid =  "accountid";
                        pid = "parentaccountid";

                        int i = sdisplay.Split(';').Length;

                        for (int j = 0; j < i; j++)
                        {
                            try { sValue += childentity.GetAttributeValue<string>(fields[j]) + " "; }
                            catch (Exception ex)
                            {
                                //s = "sValue haku: " + ex.Message.ToString();
                                //this.ReportMessage(s.ToString());
                            }
                        }
                        childitem.Name = sValue;


                        childitem.EntityName = childentity.LogicalName;
                        childitem.Level = sitm.Level;
                        childitem.ParentEntityName = sitm.EntityName;
                        childitem.Color = sitm.Color;
                        childitem.RecordId = childentity.GetEntityId<Guid>(sid);

                        try { childitem.ParentId = childentity.GetAttributeValue<Guid>(pid); }
                        catch
                        {
                            //sb.AppendLine("guid: parentid tietoa ei löydy");
                            //this.ReportMessage(sb.ToString());
                        }
                        listc.Add(childitem);

                    }

                }
            }

1 个答案:

答案 0 :(得分:3)

而不是

childentity.GetAttributeValue<Guid>(pid)

使用

childentity.GetAttributeValue<EntityReference>(pid)