Exemplo n.º 1
0
        private Expression LikeInMethod(MethodCallExpression node)
        {
            if (node.Method.DeclaringType == typeof(string))
            {
                // LIKE '..'
                if (!node.Object.IsSpecificMemberExpression(typeof(TData), EntityTableCacheHelper.TryGetPropertyList <TData>()))
                {
                    return(node);
                }

                Visit(node.Object);
                _serverWriter.Like();
                if (node.Method.Name == MethodCall.EndsWith || node.Method.Name == MethodCall.Contains)
                {
                    _serverWriter.LikePrefix();
                }
                Visit(node.Arguments[0]);
                if (node.Method.Name == MethodCall.StartsWith || node.Method.Name == MethodCall.Contains)
                {
                    _serverWriter.LikeSuffix();
                }
                return(node);
            }

            // IN (...)
            object ev;

            if (node.Method.DeclaringType == typeof(List <string>))
            {
                if (
                    !node.Arguments[0].IsSpecificMemberExpression(typeof(TData),
                                                                  EntityTableCacheHelper.TryGetPropertyList <TData>()))
                {
                    return(node);
                }


                Visit(node.Arguments[0]);
                ev = node.Object.GetValueFromExpression();
            }
            else if (node.Method.DeclaringType == typeof(Enumerable))
            {
                if (
                    !node.Arguments[1].IsSpecificMemberExpression(typeof(TData),
                                                                  EntityTableCacheHelper.TryGetPropertyList <TData>()))
                {
                    return(node);
                }

                Visit(node.Arguments[1]);
                ev = node.Arguments[0].GetValueFromExpression();
            }
            else
            {
                return(node);
            }

            _serverWriter.In();

            // Add each string in the collection to the list of locations to obtain data about.
            var queryStrings = (IList <object>)ev;
            var count        = queryStrings.Count();

            _serverWriter.OpenBrace();
            for (var i = 0; i < count; i++)
            {
                _serverWriter.Parameter(queryStrings.ElementAt(i));

                if (i + 1 < count)
                {
                    _serverWriter.Delimiter();
                }
            }
            _serverWriter.CloseBrace();

            return(node);
        }