/// <summary> /// Randomize the set of rows inside a datable /// by adding a random field and return the table sorted by that field /// </summary> /// <param name="dt">DataTable</param> /// <param name="n">Number of wanted rows</param> /// <returns>DataView containing the randomized data</returns> public static DataView Randomize(DataTable dt, int n) { System.Guid geuid = System.Guid.NewGuid(); if (!dt.Columns.Contains(lwRanDomColumn)) { dt.Columns.Add(lwRanDomColumn, geuid.GetType()); } foreach (DataRow dr in dt.Rows) { dr[lwRanDomColumn] = System.Guid.NewGuid(); } dt.AcceptChanges(); return(GetTop(new DataView(dt, "", lwRanDomColumn, DataViewRowState.CurrentRows), n, lwRanDomColumn)); }
/// <summary> /// This method is used to get attributes from a specified entity. It returns Dictionary containing all the required attributes values. /// </summary> /// <param name="entityGuid">GUID of the entity</param> /// <param name="entityName_">The entity name type (contact,lead,...)</param> ///<param name="entityAttributes">The ArrayList containing the attributes names types you want to retrieve (firstname,lastname,...)</param> public Dictionary<string, string> getEntity(Guid entityGuid, string entityName_, ArrayList entityAttributes) { Dictionary<string, string> arrayData = new Dictionary<string, string>(); try { isconnected(); ArrayList arrayResults = new ArrayList(); // Create the retrieve target. TargetRetrieveDynamic targetRetrieve = new TargetRetrieveDynamic(); // Set the properties of the target. targetRetrieve.EntityName = entityName_; targetRetrieve.EntityId = entityGuid; // Create the request object. RetrieveRequest retrieve = new RetrieveRequest(); ColumnSet col = new ColumnSet(); // Set the properties of the request object. retrieve.Target = targetRetrieve; for (int i = 0; i < entityAttributes.Count; i++) { col.AddColumn(entityAttributes[i].ToString()); } retrieve.ColumnSet = col; // Indicate that the BusinessEntity should be retrieved as a // DynamicEntity. retrieve.ReturnDynamicEntities = true; // Execute the request. RetrieveResponse retrieved = (RetrieveResponse)m_crmService.Execute(retrieve); // Extract the DynamicEntity from the request. DynamicEntity entity = (DynamicEntity)retrieved.BusinessEntity; Microsoft.Crm.Sdk.CrmDateTime crmDateTimeVar = new Microsoft.Crm.Sdk.CrmDateTime(); Microsoft.Crm.Sdk.CrmNumber crmNumberVar = new Microsoft.Crm.Sdk.CrmNumber(); Picklist crmPickList = new Picklist(); Guid crmGuid = new Guid(); Microsoft.Crm.Sdk.Key keyVar = new Microsoft.Crm.Sdk.Key(); Lookup lookupVar = new Lookup(); Microsoft.Crm.Sdk.CrmBoolean boolVar = new Microsoft.Crm.Sdk.CrmBoolean(); for (int i = 0; i < entityAttributes.Count; i++) { if (entity.Properties.Contains(entityAttributes[i].ToString())) { if (entity.Properties[entityAttributes[i].ToString()].GetType().Equals(crmDateTimeVar.GetType())) { crmDateTimeVar = (Microsoft.Crm.Sdk.CrmDateTime)entity.Properties[entityAttributes[i].ToString()]; arrayData.Add(entityAttributes[i].ToString(),crmDateTimeVar.date.ToString()); } else { if (entity.Properties[entityAttributes[i].ToString()].GetType().Equals(crmNumberVar.GetType())) { crmNumberVar = (Microsoft.Crm.Sdk.CrmNumber)entity.Properties[entityAttributes[i].ToString()]; arrayData.Add(entityAttributes[i].ToString(), crmNumberVar.Value.ToString()); } else { if (entity.Properties[entityAttributes[i].ToString()].GetType().Equals(keyVar.GetType())) { keyVar = (Microsoft.Crm.Sdk.Key)entity.Properties[entityAttributes[i].ToString()]; arrayData.Add(entityAttributes[i].ToString(), keyVar.Value.ToString()); } else { if (entity.Properties[entityAttributes[i].ToString()].GetType().Equals(lookupVar.GetType())) { lookupVar = (Microsoft.Crm.Sdk.Lookup)entity.Properties[entityAttributes[i].ToString()]; arrayData.Add(entityAttributes[i].ToString(), lookupVar.Value.ToString()); } else { if (entity.Properties[entityAttributes[i].ToString()].GetType().Equals(boolVar.GetType())) { boolVar = (Microsoft.Crm.Sdk.CrmBoolean)entity.Properties[entityAttributes[i].ToString()]; arrayData.Add(entityAttributes[i].ToString(), boolVar.Value.ToString()); } else { if (entity.Properties[entityAttributes[i].ToString()].GetType().Equals(crmPickList.GetType())) { crmPickList = (Microsoft.Crm.Sdk.Picklist)entity.Properties[entityAttributes[i].ToString()]; arrayData.Add(entityAttributes[i].ToString(), crmPickList.Value.ToString()); } else { if (entity.Properties[entityAttributes[i].ToString()].GetType().Equals(crmGuid.GetType())) { crmGuid = (Guid)entity.Properties[entityAttributes[i].ToString()]; arrayData.Add(entityAttributes[i].ToString(), crmGuid.ToString()); } else { arrayData.Add(entityAttributes[i].ToString(), entity.Properties[entityAttributes[i].ToString()].ToString()); } } } } } } } } else { arrayData.Add(entityAttributes[i].ToString(), ""); } } return arrayData; } catch (SoapException ex) { throw new InvalidPluginExecutionException("!SoapException!\n" + ex.Detail.SelectSingleNode("//description").InnerText); } catch (Exception ex) { throw new ESC_CRM_EX.getEntityException(ex.Message, entityName_, entityGuid); } }
public static object create(string strType, string strValue) { string s = ""; int i = 0; long l = 0; decimal d = (decimal)0.0; object o = null; double n = 0.0; float f = 0.0f; System.Int16 smallInt = 0; System.Guid g = System.Guid.NewGuid(); DateTime dt = DateTime.Now; if (strType == s.GetType().ToString()) { o = strValue; } else if (strType == i.GetType().ToString()) { strValue = (strValue == "NULL" ? "0" : strValue); if (strValue.Length > 0) { int I = int.Parse(strValue); o = I; } else { o = int.Parse("0"); } } else if (strType == smallInt.GetType().ToString()) { strValue = (strValue == "NULL" ? "0" : strValue); if (strValue.Length > 0) { int SI = System.Int16.Parse(strValue); o = SI; } else { o = System.Int16.Parse("0"); } } else if (strType == l.GetType().ToString()) { strValue = (strValue == "NULL" ? "0" : strValue); if (strValue.Length > 0) { long L = long.Parse(strValue); o = L; } else { o = long.Parse("0"); } } else if (strType == d.GetType().ToString()) { strValue = (strValue == "NULL" ? "0" : strValue); if (strValue.Length > 0) { decimal D = decimal.Parse(strValue); o = D; } else { o = decimal.Parse("0"); } } else if (strType == n.GetType().ToString()) { strValue = (strValue == "NULL" ? "0" : strValue); if (strValue.Length > 0) { double N = double.Parse(strValue); o = N; } else { o = double.Parse("0"); } } else if (strType == f.GetType().ToString()) { strValue = (strValue == "NULL" ? "0" : strValue); if (strValue.Length > 0) { double N = float.Parse(strValue); o = N; } else { o = float.Parse("0"); } } else if (strType == dt.GetType().ToString()) { if (strValue.Length > 0) { try { DateTime dateTime = Convert.ToDateTime(strValue); o = dateTime; } catch { o = System.DateTime.MinValue; } } else { o = System.DateTime.Now; } } else if (strType == typeof(System.DBNull).ToString()) { o = "NULL"; } else if (strType == g.GetType().ToString()) { Guid gd = new Guid(strValue); o = gd; } else if (strType == dt.GetType().ToString()) { DateTime dateTime = Convert.ToDateTime(strValue); o = dateTime; } else if (strType == "System.Single") { strValue = (strValue == "NULL" ? "0" : strValue); if (strValue.Length > 0) { decimal D = decimal.Parse(strValue); o = D; } else { o = decimal.Parse("0"); } } else if (strType == "System.Byte[]") { int length = strValue.Length; length = length / 2; o = new Byte[length]; Byte[] a = (Byte[])o; for (int j = 0; j < length; j++) { int index = j * 2; a[j] = Utilities.fromHex(strValue, index); } } else { throw new ObjectFactoryException(string.Format("ObjectFactory: Unexpected type {0}.", strType)); } return(o); }
public void TestWriteSessionId() { Guid value = new Guid(); var name = "test"; var attribute = XFireAttributeFactory.Instance.GetAttribute(value.GetType()); using (var ms = new MemoryStream()) { using (var writer = new BinaryWriter(ms)) { attribute.WriteAll(writer, name, value); } var expected = ByteHelper.CombineByteArray(new byte[]{ 0x04, 0x74, 0x65, 0x73, 0x74, 0x03 }, value.ToByteArray()); Assert.IsTrue(ms.ToArray().SequenceEqual(expected)); } }