使用.Toarray方法后排序泛型列表上 [英] sorting on generic list after using .Toarray method

查看:101
本文介绍了使用.Toarray方法后排序泛型列表上的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下code绑定一个分页数据源Repeater控件

 保护无效寻呼()
    {
        阵Q =(阵列)会议[Q];
        PagedDataSource objPds =新PagedDataSource();
        objPds.DataSource = Q;
        objPds.AllowPaging = TRUE;
        objPds.PageSize = Convert.ToInt32(ddlPageNo.SelectedValue);        objPds.CurrentPageIndex =当前页;        lblCurrentPage.Text =页面:+(当前页+ 1)的ToString()+的
           + objPds.PageCount.ToString();        //禁用preV或者如果需要的话下一个按钮
        CMD prev.Enabled = objPds.IsFirstPage!;
        cmdNext.Enabled = objPds.IsLastPage!;        rptHotels.DataSource = objPds;
        rptHotels.DataBind();    }

其中,

  getAvailableHotelResponse getres =新getAvailableHotelResponse();
  getres = objsoap.getAvailableHotel(apiKey,destinationId,CHECKIN,结账,strCurrency code,UK,假的,房间,F);
            清单<酒店及GT; HR =新的List<酒店及GT;();
            HR = getres.availableHotels.ToList();            清单< BALHotelList> BH =新的List< BALHotelList>();
            BH = h.GetHotelListByDestinationId(destinationId);
     变种Q =从波黑
                    JOIN B在a.Hotel code小时等于b.hotel code
                    排序依据a.HotelName
                    新选择
            {
                a.Hotel code,
                a.ImageURL_Text,
                a.HotelName,
                a.StarRating,
                a.HotelAddress,
                a.Destination,
                一个国家,
                a.HotelInfo,
                a.Latitude,
                a.Longitude,
                b.totalPrice,
                b.totalPriceSpecified,
                b.totalSalePrice,
                b.totalSalePriceSpecified,
                b.rooms            };
            //rptHotels.DataSource = getres.availableHotels;            会话[Q] = q.ToArray();

现在我想用

要排序的阵问: hotelname starRating

我没有找到像

任何方法

  q.sort();

  q.orderBy(Q-> hotelName)


解决方案

对你来说是的实例阵列类,而不是某种类型的数组(即 INT [] 的String [] 对象[] )。 阵列只实现的IEnumerable ,而不是的IEnumerable< T> 等等LINQ的方法是不存在的。这里的根本问题是,它是一个匿名类型的数组,比因此其他使用过真的很乱的解决方法,你不能有效地得到一个强类型数组回来了。

在这一点上最好的解决方案是创建一个新的类来保存你的数据(即酒店),而不是把它变成一个匿名类型。当您填充会话创建该类型的新实例(酒店),然后当你拉出来会话将它转换为这种类型的数组(饭店[] ),而不是普通的阵列。在这一点上,你将能够使用对象的LINQ的方法。

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)

解决方案

q for you is an instance of the Array class, rather than an array of some type (i.e. int[], string[], object[]). Array only implements IEnumerable, not IEnumerable<T> so the Linq methods aren't there. The underlying problem here is that it is an array of an anonymous type, so other than through using really messy workarounds you can't effectively get a strongly typed array back.

The best solution at this point is to create a new class to hold your data (i.e. Hotel) rather than putting it into an anonymous type. When you populate the session create new instances of that type (Hotel) , and then when you pull it out of the session cast it to an array of that type (Hotel[]), rather then the generic Array. At that point you will be able to use the Linq methods on the object.

这篇关于使用.Toarray方法后排序泛型列表上的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆