Пример #1
0
        /// <summary>
        /// Compare this object with another object.
        /// </summary>
        /// <param name="obj">The object to compare with this one.</param>
        /// <returns>
        /// A value that indicates the relative order of the objects being compared.
        /// The return value has these meanings:
        /// Less than zero - This instance precedes obj in the sort order.
        /// Zero - This instance occurs in the same position in the sort order as obj.
        /// Greater than zero - This instance follows obj in the sort order.
        /// </returns>
        public int CompareTo(object obj)
        {
            SObjectFieldType other = obj as SObjectFieldType;

            if (other == null)
            {
                return(-1);
            }

            if (this.Name == null)
            {
                return(1);
            }

            return(this.Name.CompareTo(other.Name));
        }
Пример #2
0
        /// <summary>
        /// Converts the collection of SObjects into a data table.
        /// </summary>
        /// <param name="records">The records to convert.  All of them need to be of the same type.</param>
        /// <param name="includeStructure">If true the table structure is set.  i.e. readonly columns are set.</param>
        /// <returns>The data table which holds all of the records.</returns>
        private DataTable Convert(IEnumerable <SalesForceAPI.Partner.sObject> records, bool includeStructure)
        {
            if (records == null || records.Count() == 0)
            {
                return(new DataTable());
            }

            // setup the table schema
            SalesForceAPI.Partner.sObject first = records.First();

            SObjectType objectType = null;

            if (includeStructure)
            {
                objectType = DescribeObjectType(first.type);
            }

            DataTable dt = new DataTable(first.type);

            if (first.Any != null)
            {
                foreach (System.Xml.XmlElement e in first.Any)
                {
                    if (e.HasAttributes && e.HasChildNodes)
                    {
                        for (int i = 2; i < e.ChildNodes.Count; i++)
                        {
                            string     name   = String.Format("{0}:{1}", e.LocalName, e.ChildNodes[i].LocalName);
                            DataColumn column = dt.Columns.Add(name);
                            column.ReadOnly = true;
                        }
                    }
                    else
                    {
                        DataColumn column = dt.Columns.Add(e.LocalName);
                        if (objectType != null)
                        {
                            SObjectFieldType field = objectType.Fields
                                                     .Where(f => String.Compare(f.Name, column.ColumnName, true) == 0)
                                                     .FirstOrDefault();

                            if (field != null)
                            {
                                column.ReadOnly = !field.Updateable;
                            }
                        }
                    }
                }
            }

            // convert the rows
            foreach (SalesForceAPI.Partner.sObject record in records)
            {
                if (record == null)
                {
                    throw new ArgumentException("records contains a null entry.", "records");
                }

                DataRow row = dt.NewRow();
                if (record.Any != null)
                {
                    foreach (System.Xml.XmlElement e in record.Any)
                    {
                        if (e.HasAttributes && e.HasChildNodes)
                        {
                            for (int i = 2; i < e.ChildNodes.Count; i++)
                            {
                                string name = String.Format("{0}:{1}", e.LocalName, e.ChildNodes[i].LocalName);
                                row[name] = e.ChildNodes[i].InnerText;
                            }
                        }
                        else
                        {
                            if (record.fieldsToNull != null && record.fieldsToNull.Contains(e.LocalName))
                            {
                                row[e.LocalName] = null;
                            }
                            else
                            {
                                row[e.LocalName] = e.InnerText;
                            }
                        }
                    }
                }

                dt.Rows.Add(row);
            }

            dt.AcceptChanges();

            return(dt);
        }