Ejemplo n.º 1
0
        async Task <object> getValue1(string template, IDictionary <string, object> variables, bool isVariable, object @object = null)
        {
            //枚举
            if (template.IndexOf("=>") > 0)
            {
                var segments = template.Split(new[] { "=>" }, 2, StringSplitOptions.RemoveEmptyEntries);

                var template_resource    = segments[0];
                var template_enumeration = segments[1];

                var includes = new IncludeParser(template_enumeration, templates).Parse();

                var value = await getValue2(template_resource, includes, variables, @object);

                var queryable = value is Resource
                    ? (value as Resource)?.Queryable
                    : (value as IEnumerable)?.AsQueryable();
                if (queryable == null)
                {
                    return(null);
                }

                var isArrayReturn = template_enumeration.EndsWith("[]");
                if (isArrayReturn)
                {
                    template_enumeration = template_enumeration.Substring(0, template_enumeration.Length - 2);
                }

                if (jsonqlLinqer != null)
                {
                    var selector = new SelectParser(templates).Parse(template_enumeration);
                    if (!string.IsNullOrWhiteSpace(selector))
                    {
                        queryable = jsonqlLinqer.Select(queryable, selector);
                    }
                }

                if (isArrayReturn)
                {
                    var list   = new List <object>();
                    var source = jsonqlLinqer == null?QueryableHelper.ToList(queryable) : jsonqlLinqer.ToList(queryable);

                    foreach (var item in source)
                    {
                        list.Add(await getValue1(template_enumeration, variables, false, item));
                    }
                    return(list);
                }
                else
                {
                    var first = jsonqlLinqer == null
                        ? QueryableHelper.FirstOrDefault(queryable)
                        : jsonqlLinqer.FirstOrDefault(queryable);

                    if (first == null)
                    {
                        return(null);
                    }

                    return(await getValue1(template_enumeration, variables, false, first));
                }
            }
            //取值
            else
            {
                var value = await getValue2(template, null, variables, @object);

                if (value is Resource)
                {
                    if (isVariable)
                    {
                        return(value);
                    }
                    else
                    {
                        var queryable = (value as Resource).Queryable;
                        return(jsonqlLinqer == null
                            ? QueryableHelper.ToList(queryable)
                            : jsonqlLinqer.ToList(queryable));
                    }
                }
                return(value);
            }
        }
Ejemplo n.º 2
0
 /// <summary>
 ///
 /// </summary>
 /// <param name="selector"></param>
 /// <returns></returns>
 public Resource Select(string selector)
 {
     return(new Resource(JsonqlLinqer.Select(Queryable, selector, Parameters), JsonqlIncluder, JsonqlLinqer));
 }