예제 #1
0
        /// <summary>
        /// Based on an expression, returns document field mapped from class Property.
        /// Support multi level dotted notation: x => x.Customer.Name
        /// Prefix is used on array expression like: x => x.Customers.Any(z => z.Name == "John") (prefix = "Customers."
        /// </summary>
        public string GetField(Expression expr, string prefix = "")
        {
            var property = prefix + expr.GetPath();
            var parts    = property.Split('.');
            var fields   = new string[parts.Length];
            var type     = _type;
            var isdbref  = false;

            // loop "first.second.last"
            for (var i = 0; i < parts.Length; i++)
            {
                var entity = _mapper.GetEntityMapper(type);
                var part   = parts[i];
                var prop   = entity.Members.Find(x => x.MemberName == part);

                if (prop == null)
                {
                    throw LiteException.PropertyNotMapped(property);
                }

                // if property is a IEnumerable, gets underlayer type (otherwise, gets PropertyType)
                type = prop.UnderlyingType;

                fields[i] = prop.FieldName;

                if (prop.FieldName == "_id" && isdbref)
                {
                    isdbref   = false;
                    fields[i] = "$id";
                }

                // if this property is DbRef, so if next property is _id, change to $id
                if (prop.IsDbRef)
                {
                    isdbref = true;
                }
            }

            return(string.Join(".", fields));
        }