Click here to Skip to main content
15,888,263 members
Home / Discussions / C#
   

C#

 
GeneralRe: Making charts with pdfsharp Pin
Gerry Schmitz28-Oct-14 9:45
mveGerry Schmitz28-Oct-14 9:45 
QuestionExpression to Check for a String Value in All Properties of a Type Pin
Agent__00727-Oct-14 1:42
professionalAgent__00727-Oct-14 1:42 
AnswerRe: Expression to Check for a String Value in All Properties of a Type Pin
Eddy Vluggen28-Oct-14 9:09
professionalEddy Vluggen28-Oct-14 9:09 
GeneralRe: Expression to Check for a String Value in All Properties of a Type Pin
Agent__00728-Oct-14 17:24
professionalAgent__00728-Oct-14 17:24 
GeneralRe: Expression to Check for a String Value in All Properties of a Type Pin
Eddy Vluggen29-Oct-14 8:57
professionalEddy Vluggen29-Oct-14 8:57 
AnswerRe: Expression to Check for a String Value in All Properties of a Type Pin
Alaric_28-Oct-14 10:22
professionalAlaric_28-Oct-14 10:22 
GeneralRe: Expression to Check for a String Value in All Properties of a Type Pin
Agent__00728-Oct-14 17:26
professionalAgent__00728-Oct-14 17:26 
AnswerRe: How I Got it (Partially) Done Pin
Agent__00728-Oct-14 17:08
professionalAgent__00728-Oct-14 17:08 
Anyway, now that I have managed to get it working for string and numeric properties (which are going to be "most" of them), I have been "suggested" to go with the SP approach - which was my suggestion to them in the first place. Sigh | :sigh:

If anyone is interested how I implemented the property-search (for string and numeric types only), here are the methods (please don't point out the refactoring/coding conventions, etc needed, this was just a quick one):
C#
 // This one is for searching in string types
private static Expression<Func<T, bool>> ContainsInStringTypeExpression<T>(string propertyName, string propertyValue)
        {
            var parameterExpression = Expression.Parameter(typeof(T), typeof(T).Name); // type => 

            var propertyExpression = Expression.PropertyOrField(parameterExpression, propertyName); // type.Name
            var stringTypeArray = new[] { typeof(String) };
            var containsMethod = typeof(String).GetMethod("Contains", stringTypeArray); // Contains method
            var valueToBeChecked = Expression.Constant(propertyValue, typeof(String)); // property value as a constant

            var finalContainsExpression = Expression.Call(propertyExpression, containsMethod, valueToBeChecked); // type.Name.Contains(propertyValue)

            return Expression.Lambda<Func<T, bool>>(finalContainsExpression, parameterExpression);
        }


C#
// This one is for searching in numeric types
private static Expression<Func<T, bool>> ContainsInNumericTypeExpression<T>(string propertyName, string propertyValue)
        {
            var parameterExpression = Expression.Parameter(typeof(T), typeof(T).Name); // type => 

            var propertyExpression = Expression.PropertyOrField(parameterExpression, propertyName); // type.Name
            var decimalPropertyExpression = Expression.Convert(propertyExpression, typeof(System.Nullable<Decimal>)); // (decimal?)type.Name

            // SqlFunctions.StringConvert((decimal?)type.propertyName).Contains(propertyValue),
            var nullableDecimalType = new[] { typeof(System.Nullable<Decimal>) };
            var stringConvertExp = Expression.Call(null, typeof(SqlFunctions).GetMethod("StringConvert", nullableDecimalType), decimalPropertyExpression);


            var stringTypeArray = new[] { typeof(String) };
            var containsMethod = typeof(String).GetMethod("Contains", stringTypeArray); // Contains method
            var valueToBeChecked = Expression.Constant(propertyValue, typeof(String)); // property value as a constant

            var finalContainsExpression = Expression.Call(stringConvertExp, containsMethod, valueToBeChecked); // .Contains(propertyValue)

            return Expression.Lambda<Func<T, bool>>(finalContainsExpression, parameterExpression);
        }


And finally,
C#
// A public method for passing in the search string
public static Expression<Func<T, bool>> ContainsExpression<T>(string value)
        {
            var properties = typeof(T).GetProperties();
            Expression<Func<T, bool>> finalExpression = PredicateBuilder.False<T>();
            var parameterExpression = Expression.Parameter(typeof(T), typeof(T).Name);
            string propertyName = String.Empty;
            foreach (var property in properties)
            {
                var propertyType = property.PropertyType.Name;

                if (propertyType == "String")
                {
                    var propContainsExpression = ContainsInStringTypeExpression<T>(property.Name, value);
                    finalExpression = finalExpression.Or(propContainsExpression);
                }
                else if (IsNumericType(propertyType))
                {
                    var propContainsExpression = ContainsInNumericTypeExpression<T>(property.Name, value);
                    finalExpression = finalExpression.Or(propContainsExpression);
                }
                else
                {
                    // any type other than numeric types and string are not included in the "search" functionality at the moment
                }
            }

            return finalExpression;
        }


Where, I have ORed the generated Expression using Universal Predicate Builder[^].



As always, thank you all for your suggestions.
Your time will come, if you let it be right.


modified 28-Oct-14 23:40pm.

Questioninherit a observablecollection Pin
cicill27-Oct-14 0:39
cicill27-Oct-14 0:39 
AnswerRe: inherit a observablecollection Pin
BillWoodruff27-Oct-14 1:50
professionalBillWoodruff27-Oct-14 1:50 
QuestionExtraction of data from excel to databae Pin
Member 1118244626-Oct-14 21:38
Member 1118244626-Oct-14 21:38 
AnswerRe: Extraction of data from excel to databae Pin
Pete O'Hanlon26-Oct-14 21:41
mvePete O'Hanlon26-Oct-14 21:41 
GeneralRe: Extraction of data from excel to databae Pin
Member 1118244626-Oct-14 21:58
Member 1118244626-Oct-14 21:58 
Questionsimple code for game Pin
cicill26-Oct-14 15:23
cicill26-Oct-14 15:23 
AnswerRe: simple code for game Pin
BillWoodruff26-Oct-14 16:32
professionalBillWoodruff26-Oct-14 16:32 
AnswerRe: simple code for game Pin
Mycroft Holmes26-Oct-14 19:20
professionalMycroft Holmes26-Oct-14 19:20 
AnswerRe: simple code for game Pin
Pete O'Hanlon26-Oct-14 21:38
mvePete O'Hanlon26-Oct-14 21:38 
GeneralRe: simple code for game Pin
harold aptroot27-Oct-14 4:29
harold aptroot27-Oct-14 4:29 
QuestionData binding in wpf Pin
rajeevanagaraj25-Oct-14 2:16
rajeevanagaraj25-Oct-14 2:16 
AnswerRe: Data binding in wpf Pin
Mycroft Holmes25-Oct-14 13:32
professionalMycroft Holmes25-Oct-14 13:32 
QuestionUpload and Save Image Using Webservice Pin
ASPnoob24-Oct-14 23:59
ASPnoob24-Oct-14 23:59 
AnswerRe: Upload and Save Image Using Webservice Pin
CAReed25-Oct-14 4:25
professionalCAReed25-Oct-14 4:25 
GeneralRe: Upload and Save Image Using Webservice Pin
ASPnoob25-Oct-14 7:05
ASPnoob25-Oct-14 7:05 
AnswerRe: Upload and Save Image Using Webservice Pin
Nathan Minier27-Oct-14 1:53
professionalNathan Minier27-Oct-14 1:53 
QuestionApplication could not start error 0xC0000006 starting C# .NET 2.0 program Pin
eljainc24-Oct-14 9:12
eljainc24-Oct-14 9:12 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.