class Program
{
struct point
{
public int x;
public string y;
public point(int xa,string xb)
{
x = xa;
y = xb;
}
}
static void Main(string[] args)
{
Queue<point> myp = new Queue<point>();
myp.Enqueue(new point(2, "1"));
myp.Enqueue(new point(1, "1"));
myp.Enqueue(new point(3, "1"));
//myp = (Queue<point>)myp.OrderBy 怎么用这个语法啊。
}
}
不明白3.5的扩展方法.想知道上面的地方怎么按point中的x来排序.谢谢。
foreach(point p in v)
{
Conosole.WriteLine(p.x);
}
那我必须晴空原队列myp.再从var v中一个一个加进去吗?
只能这样了?
orderby c.x
select c;
foreach (point item in xxx)
{
Console.WriteLine(item.x);
}
foreach (var p in result)
{
System.Console.WriteLine("" + p.x);
}
var result =myp.OrderBy(p => p.x);
这里返回的 result 不是var吗?匿名类型.我想得到直接得到一个强类型的。
但是会提示不能转换.why?
首先,用var不代表就是匿名类型,只是用编译器自己去确定类型而已,这里你不用var,用下面也是可以的:IOrderedEnumerable<point> result = myp.OrderBy(p => p.x);你的意思是还是要返回一个Queue<point>类型吗?
那恐怕只能自己再构造一个了,因为队列本身就是表示先进先出的集合,不存在排序的概念
恩。谢谢.我以为orderby的扩展方法.会排列后,再重新生成一个.
谢谢了。那只能这样了。orderby 的返回类型只能是实现了排序接口的类.要是能根据原数据类型,再重新new一个,再赋直.那就方便了。