高手请进:泛类型 list orderby 排序问题

李不爽 2009-08-18 09:39:44

为什么中间的 tempDS.OrderBy(od1 => od1.Value); 没有实现排序呢?
class orderData
{
public long? SId { get; set; }
public string Label { get; set; }
public double? Value { get; set; }
}
//Table排序
void tableHeader_SortHanderEvent(object sender)
{
string strSortFieled = (sender as ITableContent).Text;

List<orderData> tempDS = new List<orderData>();
orderData od;
SourceData.ToList().ForEach(s =>
{
od = new orderData();
od.SId = s.id;
DataItem ditem = s.dataItems.SingleOrDefault(di => di.XLabel==strSortFieled);
od.Label = ditem.XLabel;
od.Value = ditem.YValue;
tempDS.Add(od);
});

//tempDS.OrderBy(od1 => od1.Value); //为什么这里的排序是不成功的?未实现排序效果?

List<Series> ts = new List<Series>();
tempDS.ForEach(od2 =>
ts.Add(SourceData.SingleOrDefault(s => s.id == od2.SId))
);
this.tableData.Clear();
ts.ForEach(s => this.tableData.Add(s));
}


为什么tempDS.OrderBy(od1 => od1.Value); 这里的排序是不成功的?
如果这里改成 用循环遍历tempDS,按照大小顺序排列,如何操作?

我用debug出来的数据格式如下:
SourceData.ToList().ForEach(s => {
Debug.WriteLine(s.name);
s.dataItems.ToList().ForEach(item => Debug.WriteLine(string.Format("XLabel:{0};YValue:{1}", item.XLabel, item.YValue)));
});

1,广告费
XLabel:2009-01;YValue:0
XLabel:2009-02;YValue:0
XLabel:2009-03;YValue:0
XLabel:2009-04;YValue:34400
XLabel:2009-05;YValue:0
XLabel:2009-06;YValue:
2,业务宣传费
XLabel:2009-01;YValue:210
XLabel:2009-02;YValue:12095.08
XLabel:2009-03;YValue:971.18
XLabel:2009-04;YValue:1062.32
XLabel:2009-05;YValue:896
XLabel:2009-06;YValue:
促销费
XLabel:2009-01;YValue:0
XLabel:2009-02;YValue:2000
XLabel:2009-03;YValue:5
XLabel:2009-04;YValue:5
XLabel:2009-05;YValue:
XLabel:2009-06;YValue:
客户服务费
XLabel:2009-01;YValue:0
XLabel:2009-02;YValue:0
XLabel:2009-03;YValue:0
XLabel:2009-04;YValue:0
XLabel:2009-06;YValue:0
XLabel:2009-05;YValue:
部门使用
XLabel:2009-01;YValue:1870.04
XLabel:2009-02;YValue:0
XLabel:2009-03;YValue:558.24
XLabel:2009-04;YValue:0
XLabel:2009-06;YValue:952
XLabel:2009-05;YValue:
促销赠送
XLabel:2009-01;YValue:10267.5
XLabel:2009-02;YValue:0
XLabel:2009-03;YValue:519.83
XLabel:2009-04;YValue:0
XLabel:2009-06;YValue:0
XLabel:2009-05;YValue:
业务招待费
XLabel:2009-01;YValue:19658
XLabel:2009-02;YValue:16239
XLabel:2009-03;YValue:14296
XLabel:2009-04;YValue:11480
XLabel:2009-05;YValue:11050
XLabel:2009-06;YValue:9550
卡类
XLabel:2009-01;YValue:0
XLabel:2009-02;YValue:0
XLabel:2009-03;YValue:300
XLabel:2009-04;YValue:350
XLabel:2009-05;YValue:450
XLabel:2009-06;YValue:850
礼品
XLabel:2009-01;YValue:4238.95
XLabel:2009-02;YValue:0
XLabel:2009-03;YValue:636
XLabel:2009-04;YValue:0
XLabel:2009-05;YValue:400
XLabel:2009-06;YValue:3038.78
...全文
1306 6 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
兔子-顾问 2010-08-28
  • 打赏
  • 举报
回复
static void Main(string[] args)
{
List<orderData> temp = new List<orderData>(new orderData[] {
new orderData() { Label = "a", Value = 5, SId = 1 },
new orderData() { Label = "c", Value = 1, SId = 2 },
new orderData() { Label = "b", Value = 7, SId = 3 } });
temp = temp.OrderBy(t => t.Value).ToList();
temp.ForEach(t => Console.WriteLine("SID : " + t.SId.ToString() + ", Label : " + t.Label + ", Value : " + t.Value.ToString()));
Console.ReadKey();
}

class orderData
{
public long? SId { get; set; }
public string Label { get; set; }
public double? Value { get; set; }
}

输出
SID : 2, Label : c, Value : 1
SID : 1, Label : a, Value : 5
SID : 3, Label : b, Value : 7


你的
tempDS.OrderBy(od1 => od1.Value);
需要修改为
tempDS = tempDS.OrderBy(od1 => od1.Value).ToList();
ashley8979 2010-08-28
  • 打赏
  • 举报
回复
因为Linq 中OrderBy是延迟性的,象select,where 都是的,只有当你需要该数据的时候(象for foreach),它才会执行语句
你可以使用ToList() 或ToArray(),Count()让它不延迟,马上执行,将结果保存在操作
wwtfy 2010-08-28
  • 打赏
  • 举报
回复

//tempDS.OrderBy(od1 => od1.Value); //为什么这里的排序是不成功的?未实现排序效果?
需要赋给新的list变量
修改为下面这句就可以了
List<orderData> tempDSNew = tempDS.OrderBy(od1 => od1.Value).ToList<orderData>();
李不爽 2009-08-19
  • 打赏
  • 举报
回复
请高手指点哦!
李不爽 2009-08-18
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 lunasea0_0 的回复:]
实现以下IComparable借口
[/Quote]

高手请赐教!
lunasea0_0 2009-08-18
  • 打赏
  • 举报
回复
实现以下IComparable借口

111,092

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧