相关疑难解决方法(0)

使用LINQ进行多次排序

我从一个基本类开始,我想使用LINQ在List中操作,如下所示:

public class FooBar   
{  
    public virtual int Id { get; set; }  
    public virtual string Foo{ get; set; }  
    public virtual string Bar{ get; set; }
}
Run Code Online (Sandbox Code Playgroud)

这是我最终发现使用非lambda LINQ的东西来解决我的问题.

// code somewhere else that works and gets the desired results  
var foobarList = GetFooBarList();  // Abstracted out - returns List<Foobar>  

// Interesting piece of code that I want to examine
var resultSet = from foobars in foobarList  
                orderby foobars.Foo, foobars.Bar  
                select foobars;

// Iterate and do something interesting  
foreach …
Run Code Online (Sandbox Code Playgroud)

c# linq lambda

216
推荐指数
1
解决办法
16万
查看次数

C#List <>按x然后y排序

List <> OrderBy Alphabetical Order类似,我们希望按一个元素排序,然后按另一个元素排序.我们希望实现功能相当于

SELECT * from Table ORDER BY x, y  
Run Code Online (Sandbox Code Playgroud)

我们有一个包含许多排序函数的类,我们没有问题按一个元素排序.
例如:

public class MyClass {
    public int x;
    public int y;
}  

List<MyClass> MyList;

public void SortList() {
    MyList.Sort( MySortingFunction );
}
Run Code Online (Sandbox Code Playgroud)

我们在列表中有以下内容:

Unsorted     Sorted(x)     Desired
---------    ---------    ---------
ID   x  y    ID   x  y    ID   x  y
[0]  0  1    [2]  0  2    [0]  0  1
[1]  1  1    [0]  0  1    [2]  0  2
[2]  0  2    [1]  1  1    [1]  1 …
Run Code Online (Sandbox Code Playgroud)

.net c# sorting

87
推荐指数
4
解决办法
15万
查看次数

标签 统计

c# ×2

.net ×1

lambda ×1

linq ×1

sorting ×1