Exemple #1
0
        public static IList <ItemPedido> GetListaItemPedido(int idPedido)
        {
            var instanceEntity            = new ItemPedido();
            var properties                = instanceEntity.GetType().GetProperties();
            IList <ItemPedido> listObject = new List <ItemPedido>();

            using (var conection = BaseData.DbConnection())
            {
                string orderByCommand = string.Empty;

                SQLiteCommand sQLiteCommand = new SQLiteCommand($"SELECT * FROM {instanceEntity.GetType().Name} where idPedido = {idPedido} ", conection);
                decimal       decimalValue  = 0;
                //double doubleValue = 0;
                int      intValue  = 0;
                DateTime dateValue = DateTime.MinValue;
                object[] attributesObject;

                using (var read = sQLiteCommand.ExecuteReader())
                {
                    while (read.Read())
                    {
                        instanceEntity = new ItemPedido();

                        foreach (var item in properties)
                        {
                            attributesObject = item.GetCustomAttributes(false);
                            if (attributesObject != null && attributesObject.Length > 0 && attributesObject.Any(x => x is NotMappedAttribute))
                            {
                                continue;
                            }
                            switch (item.PropertyType.Name)
                            {
                            case "String":
                                instanceEntity.GetType().GetProperty(item.Name).SetValue(instanceEntity, Convert.ToString(read[item.Name]));
                                break;

                            case "Char":
                                int defaultCharValue = 255;                                         //Space
                                int.TryParse(Convert.ToString(read[item.Name]), out defaultCharValue);
                                instanceEntity.GetType().GetProperty(item.Name).SetValue(instanceEntity, Convert.ToChar(defaultCharValue));
                                break;

                            case "Decimal":
                                if (read[item.Name] != null && decimal.TryParse(Convert.ToString(read[item.Name]), out decimalValue))
                                {
                                    instanceEntity.GetType().GetProperty(item.Name).SetValue(instanceEntity, decimalValue);
                                }
                                break;

                            case "Int32":
                            case "Int64":
                                if (read[item.Name] != null && int.TryParse(Convert.ToString(read[item.Name]), out intValue))
                                {
                                    instanceEntity.GetType().GetProperty(item.Name).SetValue(instanceEntity, intValue);
                                }
                                break;

                            case "DateTime":
                                if (read[item.Name] != null && DateTime.TryParse(Convert.ToString(read[item.Name]), out dateValue))
                                {
                                    instanceEntity.GetType().GetProperty(item.Name).SetValue(instanceEntity, dateValue);
                                }
                                break;

                            case "Boolean":
                                if (read[item.Name] != null && int.TryParse(Convert.ToString(read[item.Name]), out intValue))
                                {
                                    bool boolValue = intValue == 1;
                                    instanceEntity.GetType().GetProperty(item.Name).SetValue(instanceEntity, boolValue);
                                }
                                break;

                            case "byte[]":
                            case "Byte[]":
                                byte[] valorByte;

                                if (read[item.Name] != null && ((read[item.Name] as byte[]) != null))
                                {
                                    valorByte = (byte[])read[item.Name];
                                    instanceEntity.GetType().GetProperty(item.Name).SetValue(instanceEntity, valorByte);
                                }

                                break;
                            }
                        }

                        listObject.Add(instanceEntity);
                    }
                    return(listObject);
                }
            }

            return(null);
        }