LINQ - How can we shorten this OrderBy statement

Asked By Burak Gunay
16-Jun-10 09:07 AM

Hello,

I have a class that has state and city string fields in it. I am sorting this class collection as follows:

   If

 

Not String.IsNullOrEmpty(sortBy) Then

 

 

    If sortBy.Contains("desc") Then

 

 

        If sortBy.Contains("city") Then

 

          retList = retList.OrderByDescending(

Function(x) x.City).ToList()

 

 

        Else

 

          retList = retList.OrderByDescending(

Function(x) x.State).ToList()

 

 

        End If

 

 

    Else

 

 

        If sortBy.Contains("city") Then

 

          retList = retList.OrderBy(

Function(x) x.City).ToList()

 

 

        Else

 

          retList = retList.OrderBy(

Function(x) x.State).ToList()

 

 

      End If

 

 

  End If

 

 

End If

 Thank you,

 Burak

 

  Sagar P replied to Burak Gunay
16-Jun-10 09:59 AM
I think your code is fine. No need to do anything in that.

Otherwise you can try using switch if you want, but i think this code is quite fine and ok......
  Burak Gunay replied to Sagar P
16-Jun-10 10:00 AM

 I guess what I am looking for is a way to sort a list dynamically using LINQ.
  Mash B replied to Burak Gunay
29-Jun-10 01:13 AM
Hi Burak,
  I have demonstrated the simple way how you can acheive sorting of class objects. Below is the code
   
  //Your Class
   
  public class Address
{
  public string City {set; get;}
  public string State {set; get;}
}
 
 
// Your code class
 
public enum SortDirection { Ascending, Decending }
 
public void Sort<TKey>(ref List<Address> list,
             Func<Address, TKey> sorter, SortDirection direction)
{
  if (direction == SortDirection.Ascending)
  list = list.OrderBy(sorter);
  else
  list = list.OrderByDescending(sorter);
}
 
//Now you can specify the field to sort when calling the Sort method.
 
Sort(address, e => e.City, SortDirection.Decending);
Create New Account
help
things like the following: var query = from s in dataContext.SomeEntities select s; if (!string.IsNullOrEmpty(inputA)) { query = from x in query where x.PropertyA = = inputA select x; } if (!string.IsNullOrEmpty(inputB)) { query = from x in query where x.PropertyB = = inputB select x; } Then enumerate query Marc (slightly simpler): if (!string.IsNullOrEmpty(inputA)) { query = query.Where(x = > x.PropertyB = = inputA); } if (!string.IsNullOrEmpty(inputB)) { query = query.Where(x = > x.PropertyB = = inputB); } Marc [watch for typo - should have been that if you ignore the query expression syntax: var query = dataContext.SomeEntities.AsQueryable(); if (!string.IsNullOrEmpty(inputA)) { query = query.Where(x = > x.PropertyA = = inputA); } if(!string.IsNullOrEmpty(inputB)) { query = query.Where(x = > x.PropertyB = = inputB); } You could even write a method to ConditionalWhere<T> (static this IQueryable<T> query, string value, Expression<Predicate<T> > predicate) { if (!string.IsNullOrEmpty(value)) { return query.Where(predicate); } } then call it with: var query = query.ConditionalWhere(valueA, x object) First time I've used a "let" in anger. . . quite handy actually ;-p static class PropertyExtensions { public static T GetPropertyValue<T> (this object component, string propertyName) { return (T) TypeDescriptor.GetProperties
them away. Any help will be appreciated! Greeting Martin. Code: namespace Edifact { using namespace std; class EdifactFormat; class ElementFormat; template <class Ch, class Tr, class In> class EdifactSibling; template <class Ch, class Tr, class In> class ElementSibling; template <class Ch, class Tr, class In> class Request; template
Template class usage C++ / VB I have a project that has a base class, inherited class and inherited class class CSession { public: BOOL Method1(); BOOL Method2(); }; class CHtml : public CSession { BOOL Method1(); BOOL Method2(); }; class CClient : public CHtml { BOOL Method1(); BOOL Method2(); }; I would like to Inherit the CHtml class from a different base class class CSessionSSL : CSecure { BOOL Method1(); BOOL Method2(); }; class CHtmlSSL : public
difference between private class nad sealed class what is the difference between private class nad sealed class? hi, A class which restricts inheritance for security region is called Sealed class. Sealed class is the last class in hierarchy. Sealed class can be a derived class but can't be a base