Exemple #1
0
        /// <summary>
        /// Multi condition filter with main filter on company name like
        /// </summary>
        /// <param name="pNameCondition">Company name like text</param>
        /// <param name="pName">Text to filter on company name</param>
        /// <param name="pContactType">contact type from contact type reference table</param>
        /// <returns></returns>
        public List <CustomerEntity> GetCustomersLikeCustomerEntities(LikeOptions pNameCondition, string pName, int pContactType)
        {
            var nameFilter = "";

            if (pNameCondition == LikeOptions.StartsWith)
            {
                nameFilter = $"{pName}%";
            }
            else if (pNameCondition == LikeOptions.Contains)
            {
                nameFilter = $"%{pName}%";
            }
            else if (pNameCondition == LikeOptions.EndsWith)
            {
                nameFilter = $"{pName}%";
            }

            using (var context = new NorthWindContext())
            {
                var customerData = (
                    from customer in context.Customers
                    join contactType in context.ContactType on customer.ContactTypeIdentifier equals contactType.ContactTypeIdentifier
                    join contact in context.Contact on customer.ContactIdentifier equals contact.ContactIdentifier
                    where Functions.Like(customer.CompanyName, nameFilter)
                    select new CustomerEntity
                {
                    CustomerIdentifier = customer.CustomerIdentifier,
                    CompanyName = customer.CompanyName,
                    ContactIdentifier = customer.ContactIdentifier,
                    FirstName = contact.FirstName,
                    LastName = contact.LastName,
                    ContactTypeIdentifier = contactType.ContactTypeIdentifier,
                    ContactTitle = contactType.ContactTitle,
                    City = customer.City,
                    PostalCode = customer.PostalCode,
                    CountryIdentifier = customer.CountryIdentfier,
                    CountyName = customer.CountryIdentfierNavigation.CountryName
                }).ToList();

                if (pContactType > 0)
                {
                    customerData = customerData.Where(x => x.ContactTypeIdentifier == pContactType).ToList();
                }
                return(customerData);
            }
        }
        /// <summary>
        /// Set like option Contains, StartWith or EndWith
        /// </summary>
        /// <param name="strValue"></param>
        /// <param name="option"></param>
        /// <returns></returns>
        internal static string SetOption(string strValue, LikeOptions option)
        {
            switch (option)
            {
            case LikeOptions.Contains:
            {
                strValue = string.Join("", '%', strValue, '%');
                break;
            }

            case LikeOptions.StartWith:
            {
                strValue = string.Join("", strValue, '%');
                break;
            }

            case LikeOptions.EndWith:
            {
                strValue = string.Join("", '%', strValue);
                break;
            }
            }
            return(strValue);
        }
        /// <summary>
        /// Property value like start, end or contains strValue expression
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="parameter"></param>
        /// <param name="property"></param>
        /// <param name="strValue"></param>
        /// <returns></returns>
        internal static MethodCallExpression LikeExpression <TSource>(ParameterExpression parameter, string property, string strValue, LikeOptions option)
        {
            MethodCallExpression body = Expression.Call(typeof(DbFunctionsExtensions).GetMethod(nameof(DbFunctionsExtensions.Like),
                                                                                                new[] {
                typeof(DbFunctions), typeof(string), typeof(string)
            }),
                                                        Expression.Constant(EF.Functions),
                                                        Expression.Property(parameter, typeof(TSource).GetProperty(property)),
                                                        Expression.Constant(SetOption(strValue, option)));

            return(body);
        }
        /// <summary>
        /// Property value like start, end or contains words expression
        /// </summary>
        /// <param name="props"></param>
        /// <param name="words"></param>
        /// <param name="option"></param>
        /// <param name="operators"></param>
        /// <returns></returns>
        internal static Expression <Func <TSource, bool> > LikesExpression <TSource>(string[] props, string[] words, LikeOptions option, Operators operators)
        {
            Expression          result    = null;
            ParameterExpression parameter = Expression.Parameter(typeof(TSource));

            foreach (var prop in props)
            {
                Expression expr = words
                                  .Select(word =>
                                          LikeExpression <TSource>(parameter, prop, word, option)
                                          )
                                  .Aggregate <MethodCallExpression, Expression>(null, (current, call) => current != null ? ConcatExpression(current, call, operators) : (Expression)call);
                result = result.ConcatExpression(expr, operators);
            }
            return(Expression.Lambda <Func <TSource, bool> >(result, parameter));
        }
Exemple #5
0
        }        /// <summary>

        ///  Where property value like %strValue% extension
        /// </summary>
        /// <typeparam name="TSource"></typeparam>
        /// <param name="source"></param>
        /// <param name="property"></param>
        /// <param name="strValue"></param>
        /// <returns></returns>
        public static IQueryable <TSource> WhereLike <TSource>(this IQueryable <TSource> source,
                                                               string property, string strValue, LikeOptions option) =>
        source.WhereFiltered(new List <Filter> {
            new Filter
            {
                Key        = property,
                Value      = strValue,
                Comparison = option.ParseEnum()
            }
        });
Exemple #6
0
 private static ComparisonOperators ParseEnum(this LikeOptions value)
 {
     return((ComparisonOperators)Enum.Parse(typeof(ComparisonOperators), value.ToString("G"), true));
 }