Пример #1
0
        ///// <summary>
        /////
        ///// </summary>
        ///// <param name="db"></param>
        ///// <param name="format"></param>
        ///// <param name="parameters"></param>
        ///// <returns></returns>
        //internal static Task<JArray> FromSqlToJsonAsync(
        //    this ObjectContext db,
        //    string format,
        //    params object[] parameters)
        //{
        //    return FromSqlToJsonAsync(db, TemplateQuery.FromString(format, parameters));
        //}


        /// <summary>
        ///
        /// </summary>
        /// <param name="db"></param>
        /// <param name="query"></param>
        /// <returns></returns>
        public static async Task <JArray> FromSqlToJsonAsync(
            this ObjectContext db,
            TemplateQuery query)
        {
            JArray list  = new JArray();
            var    props = new List <(int index, string name)>();

            using (var dbReader = await DbReader.CreateAsync(db, query))
            {
                var reader = dbReader.Reader;
                while (await reader.ReadAsync())
                {
                    if (props.Count == 0)
                    {
                        if (reader.FieldCount == 0)
                        {
                            return(list);
                        }
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            var n = reader.GetName(i);
                            props.Add((i, n));
                        }
                    }

                    var item = new JObject();

                    foreach (var(index, n) in props)
                    {
                        var value = reader.GetValue(index);
                        if (value == null || value == DBNull.Value)
                        {
                            continue;
                        }
                        item.Add(n, JToken.FromObject(value));
                    }
                    list.Add(item);
                }
                return(list);
            }
        }
Пример #2
0
        public static async Task <List <T> > FromSqlAsync <T>(
            this ObjectContext db,
            TemplateQuery query,
            bool ignoreUnmatchedProperties = false)
            where T : class
        {
            List <T> list  = new List <T>();
            var      props = new List <(int index, PropertyInfo property, string name)>();

            using (var dbReader = await DbReader.CreateAsync(db, query))
            {
                var reader = dbReader.Reader;
                while (await reader.ReadAsync())
                {
                    if (props.Count == 0)
                    {
                        if (reader.FieldCount == 0)
                        {
                            return(list);
                        }
                        Type          type     = typeof(T);
                        List <string> notFound = new List <string>();
                        for (int i = 0; i < reader.FieldCount; i++)
                        {
                            var n   = reader.GetName(i);
                            var key = $"{type.FullName}.{n}";
                            var p   = propertyCache.GetOrAdd(key, a => type.GetProperty(n, BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.SetProperty | BindingFlags.Public));
                            props.Add((i, p, n));
                        }

                        var empty = props.Where(x => x.property == null).Select(x => x.name);
                        if (empty.Any())
                        {
                            if (!ignoreUnmatchedProperties)
                            {
                                throw new InvalidOperationException($"Properties {string.Join(",", empty)} not found in {type.FullName}");
                            }
                            props = props.Where(x => x.property != null).ToList();
                        }
                    }

                    var item = Activator.CreateInstance <T>();

                    foreach (var(index, property, n) in props)
                    {
                        var value = reader.GetValue(index);
                        if (value == null || value == DBNull.Value)
                        {
                            continue;
                        }
                        var type = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType;
                        if (value.GetType() != type)
                        {
                            value = Convert.ChangeType(value, type);
                        }

                        property.SetValue(item, value);
                    }
                    list.Add(item);
                }
                return(list);
            }
        }