Click here to Skip to main content
15,886,362 members
Home / Discussions / C#
   

C#

 
AnswerRe: Unexpected naming of client for a WCF service Pin
Russell Bill26-Oct-12 11:47
Russell Bill26-Oct-12 11:47 
QuestionReading from XML file Pin
gibsray26-Oct-12 5:32
gibsray26-Oct-12 5:32 
AnswerRe: Reading from XML file Pin
Ravi Bhavnani26-Oct-12 5:47
professionalRavi Bhavnani26-Oct-12 5:47 
AnswerRe: Reading from XML file Pin
fjdiewornncalwe26-Oct-12 6:09
professionalfjdiewornncalwe26-Oct-12 6:09 
AnswerRe: Reading from XML file Pin
Clifford Nelson26-Oct-12 8:56
Clifford Nelson26-Oct-12 8:56 
AnswerRe: Reading from XML file Pin
gopal pradhan8-Nov-12 23:30
gopal pradhan8-Nov-12 23:30 
QuestionReflection and Linq Extension Methods Pin
Gustavo Ushijima26-Oct-12 3:53
Gustavo Ushijima26-Oct-12 3:53 
AnswerRe: Reflection and Linq Extension Methods Pin
onelopez26-Oct-12 4:19
onelopez26-Oct-12 4:19 
There you go, sorts on properties of properties... not quite sure if it works with methods. The logic is there, but it's not fully tested
C#
public static IEnumerable<T> SortObject<T>(this IEnumerable<T> ObjectToSort, string SortBy)
   {
       if (ObjectToSort == null)
           return null;
       if (string.IsNullOrEmpty(SortBy) == true)
           return null;

       // Handle sorting on object methods as well as properties
       string[] tempSorts = SortBy.Split(',');
       List<string> SortList = new List<string>();
       bool inParanthese = false;
       string addString = string.Empty;
       foreach (string tempSort in tempSorts)
       {
           if ((tempSort.Contains("(") == true) && (tempSort.Contains(")") == false))
           {
               inParanthese = true;
           }
           else if (tempSort.Contains(")") == true)
           {
               inParanthese = false;
           }
           if (inParanthese == false)
           {
               if (string.IsNullOrEmpty(addString) == false)
               {
                   SortList.Add(addString + "," + tempSort.Trim());
               }
               else
               {
                   SortList.Add(tempSort);
               }
               addString = string.Empty;
           }
           else
           {
               if (string.IsNullOrEmpty(addString) == false)
                   addString += ",";
               addString += tempSort.Trim();
           }
       }
       string[] Sorts = SortList.ToArray();
       bool Sorted = false;
       foreach (string FieldSort in Sorts)
       {
           string[] SubSort = FieldSort.Trim().Split(' ');

           string[] props = SubSort[0].Split('.');
           Type type = typeof(T);
           ParameterExpression arg = Expression.Parameter(type, "x");
           Expression expr = arg;
           foreach (string prop in props)
           {
               System.Reflection.PropertyInfo pi = type.GetProperty(prop);
               if (pi != null)
               {
                   Sorted = true;
                   //expr = Expression.Property(expr, pi);
                   expr = Expression.MakeMemberAccess(expr, pi);
                   type = pi.PropertyType;
               }
               else
               {
                   // Handle sorting on object methods as well as properties
                   string wholeMethod = prop;
                   if ((wholeMethod.Contains("(") == false) && (wholeMethod.Contains(")") == false))
                   {
                       //Ensure that method is in the proper format
                       wholeMethod += "()";
                   }
                   int firstParans = wholeMethod.IndexOf('(');
                   int lastParans = wholeMethod.LastIndexOf(')');
                   if ((firstParans > -1) && (lastParans > firstParans))
                   {
                       //Parse out method name and parameters
                       string methodname = wholeMethod.Substring(0, firstParans).Trim();
                       string paramList = wholeMethod.Substring(firstParans + 1, lastParans - (firstParans + 1));

                       System.Reflection.MethodInfo mi = null;
                       List<string> indParams = null;
                       if (paramList.Length > 0)
                       {
                           //Found some parameters
                           bool inDoubleQuotes = false;
                           bool inSingleQuotes = false;
                           string currParam = string.Empty;
                           indParams = new List<string>();
                           //Parse each individual parameter into a list of strings
                           foreach (char c in paramList)
                           {
                               if (c == '\"')
                               {
                                   if (inSingleQuotes == false)
                                   {
                                       inDoubleQuotes = !inDoubleQuotes;
                                   }
                                   currParam += c;
                               }
                               else if (c == '\'')
                               {
                                   if (inDoubleQuotes == false)
                                   {
                                       inSingleQuotes = !inSingleQuotes;
                                   }
                                   currParam += c;
                               }
                               else if ((c == ',') && (inDoubleQuotes == false) && (inSingleQuotes == false))
                               {
                                   //end of parameter
                                   if (string.IsNullOrEmpty(currParam) == false)
                                   {
                                       indParams.Add(currParam);
                                       currParam = string.Empty;
                                   }
                               }
                               else
                               {
                                   currParam += c;
                               }
                           }
                           if ((string.IsNullOrEmpty(currParam) == false) && (inDoubleQuotes == false) &&
                               (inSingleQuotes == false))
                           {
                               indParams.Add(currParam);
                           }
                           Type[] methodTypes = new Type[indParams.Count];
                           Expression[] methodParams = new Expression[indParams.Count];
                           int paramTypeIndex = 0;
                           int intResult;
                           //Determine parameter types and values for each parameter
                           foreach (string indParam in indParams)
                           {
                               if (indParam[0] == '\'')
                               {
                                   if ((indParam.Length == 3) && (indParam[2] == '\''))
                                   {
                                       //char
                                       methodTypes[paramTypeIndex] = typeof(char);
                                       methodParams[paramTypeIndex] = Expression.Constant(indParam[1], typeof(char));
                                   }
                                   else
                                   {
                                       //Invalid char so do as string
                                       methodTypes[paramTypeIndex] = typeof(string);
                                       methodParams[paramTypeIndex] = Expression.Constant(indParam, typeof(string));
                                   }
                               }
                               else if (indParam[0] == '"')
                               {
                                   //string
                                   methodTypes[paramTypeIndex] = typeof(string);
                                   methodParams[paramTypeIndex] = Expression.Constant(indParam, typeof(string));
                               }
                               else if (int.TryParse(indParam, out intResult) == true)
                               {
                                   //Int
                                   methodTypes[paramTypeIndex] = typeof(int);
                                   methodParams[paramTypeIndex] = Expression.Constant(intResult, typeof(int));
                               }
                               else
                               {
                                   methodTypes[paramTypeIndex] = typeof(object);
                                   methodParams[paramTypeIndex] = Expression.Constant(indParam);
                               }
                               paramTypeIndex++;
                           }
                           //Get the method
                           mi = type.GetMethod(methodname, methodTypes);
                           if (mi != null)
                           {
                               //Add the method with parameters to the dynamic LINQ expression
                               expr = Expression.Call(expr, mi, methodParams);
                           }
                       }
                       else
                       {
                           //No parameters for this method
                           mi = type.GetMethod(methodname, System.Type.EmptyTypes);
                           if (mi != null)
                           {
                               //Add the method to the dynamic LINQ expression
                               expr = Expression.Call(expr, mi);
                           }
                       }
                   }
               }
           }
           LambdaExpression lambda = Expression.Lambda(expr, arg);
           if (Sorted == true)
           {
               //Sorted = true;
               IQueryable<T> queryableData = ObjectToSort.AsQueryable<T>();
               if ((SubSort.Count() > 1) && (string.Compare(SubSort[1].Trim(), "desc", true) == 0))
               {
                   MethodCallExpression MyExpr = Expression.Call(
                       typeof(Queryable), "OrderByDescending",
                       new Type[] { typeof(T), type },
                       queryableData.Expression, lambda);
                   IQueryable<T> results = queryableData.Provider.CreateQuery<T>(MyExpr);
                   try
                   {
                       //Trying to determine when sub-property is a nullable object how to
                       //get the sort results without a NullReferenceException error
                       //Getting the count() throws the error if the sort is performed on a
                       //sub-property of a nullable object that is the property of the main object
                       results.Count();
                       ObjectToSort = results;
                   }
                   catch (Exception exc){}
                   //ObjectToSort = ObjectToSort.OrderByDescending(lambda);
                   //ObjectToSort = ObjectToSort.OrderByDescending(i => propInfo.GetValue(i, null));
               }
               else
               {
                   MethodCallExpression MyExpr = Expression.Call(
                       typeof(Queryable), "OrderBy",
                       new Type[] { typeof(T), type },
                       queryableData.Expression, Expression.Quote(lambda));
                   IQueryable<T> results = queryableData.Provider.CreateQuery<T>(MyExpr);
                   try
                   {
                       //Trying to determine when sub-property is a nullable object how to
                       //get the sort results without a NullReferenceException error
                       //Getting the count() throws the error if the sort is performed on a
                       //sub-property of a nullable object that is the property of the main object
                       results.Count();
                       ObjectToSort = results;
                   }
                   catch (Exception exc){}
                   //ObjectToSort = ObjectToSort.OrderBy(x => expr);
                   //ObjectToSort = ObjectToSort.OrderBy(i => propInfo.GetValue(i, null));
               }
           }
       }
       if (Sorted == true)
       {
           //Found at least one property to sort on in SortBy string
           return ObjectToSort;
       }
       else
       {
           //No valid properties found to sort on in SortBy string
           return null;
       }
   }

QuestionPrinting Automatically When A Particular Event Occurs Pin
Matt U.26-Oct-12 3:43
Matt U.26-Oct-12 3:43 
AnswerRe: Printing Automatically When A Particular Event Occurs Pin
Eddy Vluggen26-Oct-12 4:48
professionalEddy Vluggen26-Oct-12 4:48 
GeneralRe: Printing Automatically When A Particular Event Occurs Pin
Matt U.26-Oct-12 6:54
Matt U.26-Oct-12 6:54 
GeneralRe: Printing Automatically When A Particular Event Occurs Pin
Matt U.26-Oct-12 7:36
Matt U.26-Oct-12 7:36 
AnswerRe: Printing Automatically When A Particular Event Occurs Pin
Clifford Nelson26-Oct-12 9:02
Clifford Nelson26-Oct-12 9:02 
QuestionADO.NET 4 SqlConnection SqlDataAdapter Pin
devvvy25-Oct-12 23:34
devvvy25-Oct-12 23:34 
AnswerRe: ADO.NET 4 SqlConnection SqlDataAdapter Pin
Deflinek25-Oct-12 23:43
Deflinek25-Oct-12 23:43 
AnswerRe: ADO.NET 4 SqlConnection SqlDataAdapter Pin
Pete O'Hanlon25-Oct-12 23:55
mvePete O'Hanlon25-Oct-12 23:55 
GeneralRe: ADO.NET 4 SqlConnection SqlDataAdapter Pin
devvvy26-Oct-12 2:58
devvvy26-Oct-12 2:58 
GeneralRe: ADO.NET 4 SqlConnection SqlDataAdapter Pin
Eddy Vluggen26-Oct-12 3:33
professionalEddy Vluggen26-Oct-12 3:33 
GeneralRe: ADO.NET 4 SqlConnection SqlDataAdapter Pin
devvvy28-Oct-12 20:00
devvvy28-Oct-12 20:00 
AnswerRe: ADO.NET 4 SqlConnection SqlDataAdapter Pin
Simon_Whale26-Oct-12 0:31
Simon_Whale26-Oct-12 0:31 
Question[Help] Sending message automatically Pin
askersuku25-Oct-12 19:18
askersuku25-Oct-12 19:18 
AnswerRe: [Help] Sending message automatically Pin
Pete O'Hanlon25-Oct-12 23:51
mvePete O'Hanlon25-Oct-12 23:51 
QuestionTextBox not recognising Input Pin
chappie425-Oct-12 14:43
chappie425-Oct-12 14:43 
AnswerRe: TextBox not recognising Input Pin
Eddy Vluggen25-Oct-12 15:08
professionalEddy Vluggen25-Oct-12 15:08 
GeneralRe: TextBox not recognising Input Pin
chappie425-Oct-12 15:15
chappie425-Oct-12 15:15 

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.