Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sorting on generic list after using .Toarray method

I am using following code to bind a paged datasource to repeater control

  protected void Paging()
    {
        Array q = (Array)Session["q"];
        PagedDataSource objPds = new PagedDataSource();
        objPds.DataSource = q;
        objPds.AllowPaging = true;
        objPds.PageSize = Convert.ToInt32(ddlPageNo.SelectedValue);

        objPds.CurrentPageIndex = CurrentPage;

        lblCurrentPage.Text = "Page: " + (CurrentPage + 1).ToString() + " of "
           + objPds.PageCount.ToString();

        // Disable Prev or Next buttons if necessary
        cmdPrev.Enabled = !objPds.IsFirstPage;
        cmdNext.Enabled = !objPds.IsLastPage;

        rptHotels.DataSource = objPds;
        rptHotels.DataBind();

    }

where q is

 getAvailableHotelResponse getres = new getAvailableHotelResponse();    
  getres = objsoap.getAvailableHotel(apiKey, destinationId, checkIn, checkOut, strCurrencyCode, "UK", false, rooms, f);   
            List<hotel> hr = new List<hotel>();
            hr = getres.availableHotels.ToList();

            List<BALHotelList> bh = new List<BALHotelList>();
            bh = h.GetHotelListByDestinationId(destinationId);
     var q = from a in bh
                    join b in hr on a.HotelCode equals b.hotelCode
                    orderby a.HotelName
                    select new
            {
                a.HotelCode,
                a.ImageURL_Text,
                a.HotelName,
                a.StarRating,
                a.HotelAddress,
                a.Destination,
                a.Country,
                a.HotelInfo,
                a.Latitude,
                a.Longitude,
                b.totalPrice,
                b.totalPriceSpecified,
                b.totalSalePrice,
                b.totalSalePriceSpecified,
                b.rooms

            };


            //rptHotels.DataSource = getres.availableHotels;

            Session["q"] = q.ToArray();

now i want to use

want to sort the array q by hotelname or starRating .

I am not finding any method like

q.sort(); 

or

q.orderBy(q->hotelName)
like image 535
rahularyansharma Avatar asked Nov 28 '25 04:11

rahularyansharma


2 Answers

For an in-place sort of an existing array by a member:

Array.Sort(theArray, (x,y) => string.Compare(x.HotelName, y.HotelName));
like image 87
Marc Gravell Avatar answered Nov 30 '25 18:11

Marc Gravell


use following..

q.OrderBy(x => x.HotelName);

UPDATE

casting back from session, do it like this

//if you have concrete type instead of object, use that type

var t = (IEnumerable<object>)Session["q"];

UPDATE 2

your projection should be a concrete type (i.e make a new Hotel class to represent your projection) , otherwise you would not be able to do OrderBy on some property of your projection

like image 37
Anand Avatar answered Nov 30 '25 16:11

Anand



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!