如何在C#中使用属性链接列表? [英] How used property linked list in c#?

查看:123
本文介绍了如何在C#中使用属性链接列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好!
如何订购链表,但此链表未订购!

Hello!
How do I order linked list,but this linked list did''t order!

LinkedList<double> arrayresultMemberShip = new LinkedList<double>();
arrayresultMemberShip.AddLast(1);
arrayresultMemberShip.AddLast(0.49);
arrayresultMemberShip.OrderBy(i => i);

请帮助我如何使用OrderBy?

please help me how used OrderBy?

推荐答案

OrderBy返回一个指向有序集合的新值;它不会对集合进行重新排序:

OrderBy returns a new value that points to the ordered collection; it does not reorder the collection in place:

LinkedList<double> arrayresultMemberShip = new LinkedList<double>();
arrayresultMemberShip.AddLast(1);
arrayresultMemberShip.AddLast(0.49);
arrayresultMemberShip.OrderBy(i => i);

Console.WriteLine("---- Original ----");
foreach (double d in arrayresultMemberShip)
{
    Console.WriteLine(d);
}

IOrderedEnumerable<double> orderedList = arrayresultMemberShip.OrderBy(i => i);

Console.WriteLine("---- Ordered ----");
foreach (double d in orderedList)
{
    Console.WriteLine(d);
}


我希望您看到了MSDN帮助页面和代码示例:
I hope you saw the MSDN help page and code sample: http://msdn.microsoft.com/en-us/library/bb534966.aspx[^].

I suspect you don''t understand (generics) delegates, lambda syntax (yes, for this simple application understanding syntax is quite enough) or maybe IEnumerable.
Lets see. You are required to supply a parameter, which is the method with the signature defined by the delegate type Func<TSource, TKey>. In your case, this is a function which accept one parameter which is of the type of your list element (double) and returns some key of the type for which the ordering operation is defined. So, your code sample is correct, if you need to return a collection in natural order.

Without the lambda, you could write equivalent code:
static double GetOrderingCriterionKey(double value) { return value; }

//...
IOrderedEnumerable<double> =
   arrayresultMembership.OrderBy(new Func<double,>(GetOrderingCriterionKey));



如果需要更改顺序,请使用其他OrderBy方法(使用IComparer的方法),请参见



If you need to change the order, use other OrderBy methods, those using IComparer, see http://msdn.microsoft.com/en-us/library/he2s3bh7%28v=VS.100%29.aspx[^].

See also the definition of the generic delegate type Func. In your case, it is instantiated as

delegate double DoubleFunction(double element);



关于IEnumerableIOrderedEnumerable,只需阅读并了解以下内容:
http://msdn .microsoft.com/en-us/library/system.collections.ienumerable.aspx [ http://msdn.microsoft.com/en-us/library/bb534852.aspx [ ^ ].

—SA



About IEnumerable and IOrderedEnumerable, just read and understand this: http://msdn.microsoft.com/en-us/library/system.collections.ienumerable.aspx[^],
http://msdn.microsoft.com/en-us/library/bb534852.aspx[^].

—SA


这篇关于如何在C#中使用属性链接列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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