如何在C#中使用属性链接列表? [英] How used property linked list in 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帮助页面和代码示例: ^ ].
我怀疑您不了解(泛型)委托,lambda语法(是的,对于这个简单的应用程序来说,了解语法就足够了)或也许IEnumerable
.
让我们来看看.您需要提供一个参数,该参数是具有由委托类型Func<TSource, TKey>
定义的签名的方法.在您的情况下,这是一个函数,该函数接受一个列表元素类型的参数(双精度),并返回定义了排序操作的类型的某个键. 因此,如果您需要按自然顺序返回集合,则您的代码示例是正确的.
没有lambda,您可以编写等效代码:
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 maybeIEnumerable
.
Lets see. You are required to supply a parameter, which is the method with the signature defined by the delegate typeFunc<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
的方法),请参见 ^ ].
另请参见通用委托类型Func
的定义.在您的情况下,它被实例化为
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);
关于IEnumerable
和IOrderedEnumerable
,只需阅读并了解以下内容: http://msdn .microsoft.com/en-us/library/system.collections.ienumerable.aspx [ http://msdn.microsoft.com/en-us/library/bb534852.aspx [ ^ ].
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[^].
这篇关于如何在C#中使用属性链接列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!