1

I have a list of objects of type }

I need to sort the list of

Can I do this dynamically.

Please help.

1
  • Based on your below comments it sounds very likely that you are not correctly populating your properties. Your core issue is probably not with the ordering. If you want an answer to your question you need to post the code that sets your properties. Commented Nov 18, 2013 at 5:09

2 Answers 2

2

LINQ has OrderBy / OrderByDescending methods which make this type of sorting trivial e.g.

var orderByBookingRef = listOfRestaurants.OrderBy(x => x.Booking.BookingRef);
var orderByLatestBooking = listOfRestaurants.OrderByDescending(x => x.Booking.BookingDateTime);
var orderByLatestReview = listOfRestaurants.OrderByDescending(x => x.RestReview.ReviewDate);

The above is known as Lambda expressions which are a bit better in terms of readability, if you prefer pure LINQ syntax

var orderByBookingRef = from r in listOfRestaurants
                        orderby r.Booking.BookingRef
                        select r;
var orderByLatestBooking = from r in listOfRestaurants
                           orderby r.Booking.BookingDateTime descending
                           select r;
var orderByLatestReview = from r in listOfRestaurants
                          orderby r.RestReview.ReviewDate descending
                          select r;
Sign up to request clarification or add additional context in comments.

6 Comments

Hi James Thank you for your reply. I already tried the first solution (lambda expression.) I get this error "Check to determine if the object is null before calling the method." I used istOfRestaurants.OrderBy(x => x.Booking.BookingRef); the above mentioned error is coming because X has all the objects (Booking, Restaurant and RestaurantReview) as null. Don't know why.
Hi James Thank you for your reply. I already tried the first solution (lambda expression.) I get this error "Check to determine if the object is null before calling the method." I used istOfRestaurants.OrderBy(x => x.Booking.BookingRef); the above mentioned error is coming because X has all the objects (Booking, Restaurant and RestaurantReview) as null. Tried the LINQ expression, gives same error.
Actually my aim was to do it dynamically by constructing the sort expression. but even the basic lambda is not working.
If the objects are null, then you're probably doing something wrong. Can you post the code that instantiates the objects and tries to do the sorting?
@user2921973 You have plagued this thread with unnecessary code, if you want to show example code edit your original question. Also, if the inner classes are null then clearly they aren't being set. Update your question with some example code which reproduces the problem and it might make the issue a bit clearer.
|
0

And the other form of linq:

var sorted_by_prop = 
        from some_object in list_of_rest
        orderby some_object.prop_name_here descending // or ignore descending if you don't want it descending
        select some_object;

Specific case, assuming your list is called MyList, by Restaurant.RestaurantId field:

var sorted_list = 
        from rest in MyList
        orderby rest.Restaurant.RestaurantId
        select rest;

Will return the list by the Id of the restaurant, ascending


As per comment: You can do

var sorted_list = 
        from rest in MyList
        where rest.Booking != null && // same for the rest
        orderby rest.Restaurant.RestaurantId
        select rest;

this way it'll skip the ones that are null ...

1 Comment

Thanks Noctis, I get this error "Check to determine if the object is null before calling the method. use the new keyword to create an object instance" var sorted_list = from rest in listR orderby rest.Restaurant.RestaurantId select rest; the above mentioned error is coming because rest has all the objects (Booking, Restaurant and RestaurantReview) as null. Though original listR has all objects correctly populated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.