c# - 根据内部列表使用 .notation 对对象列表进行排序

标签 c# sorting linq-to-objects

我有这些类(class):

[DataContract]
public class RowData
{
    [DataMember]
    public string ID { get; set; }
    [DataMember]
    public string AccountName { get; set; }
    [DataMember]
    public string AuthServerName { get; set; }
    [DataMember]
    public string SecurityName { get; set; }
    [DataMember]
    public string LastUser { get; set; }
    [DataMember]
    public string Status { get; set; }
    [DataMember]
    public string ClaimRelease { get; set; }
    [DataMember]
    public List<GameItem> Games { get; set; }
}

[DataContract]
public class GameItem
{
    [DataMember]
    public string Name { get; set; }
    [DataMember]
    public string Version { get; set; }
}

如果我知道自己在做什么,我会尽可能地猜测它“应该”是什么样子。

public List<RowData> GetSortedByColumnList(Entities db, int ColumnID, string direction, int GameCount, List<RowData> rows)
{
...
    else if ((ColumnID > 3) && (ColumnID < GameCount + 4))
    {
        List<Game> gameslist = db.Games
            .Where(x => x.GameIsActive == 1)
            .ToList(); // Game has the key names

        int index = (ColumnID - GameCount) - 1; 

        // gamename is the name that i'd like to match for sorting purposes.
        string gamename = gameslist[index].GameName; 

        // *** LOOK HERE ****
        List<RowData> sortedList = rows
            .OrderBy(x => x.Games
                .Min(Games => Games.Version)
                .Where(Games => Games.Name == gamename))
            .ToList();

    rows = sortedList;
}

这是行的示例:

rows[0].id = 1
...
rows[0].Games[0].Name="A0"
rows[0].Games[0].Version=3
rows[0].Games[1].Name="B0"
rows[0].Games[1].Version=4

rows[1].id = 2
...
rows[1].Games[0].Name="A0"
rows[1].Games[0].Version=1
rows[0].Games[1].Name="B0"
rows[0].Games[1].Version=2

rows[2].id = 3
...
rows[2].Games[0].Name="A0"
rows[2].Games[0].Version=5

因此,如果字符串游戏名称等于“B0”,我希望根据版本号对列表进行排序,以便排序顺序为行[1]、行[2]、行[3]。注意 rows[2] 中没有 B0。

如何修改 //*** LOOK HERE **** 下的查询以使列表按照我的需要排序?

Compile Error 1 'char' does not contain a definition for 'Name' and no extension method 'Name' accepting a first argument of type 'char' could be found (are you missing a using directive or an assembly reference?)

最佳答案

假设您正在寻找一个查询,该查询将按版本顺序返回具有特定名称的游戏列表,您可能正在寻找类似于以下内容的内容:

List<GameItem> sortedList = rows
    .SelectMany(r => r.Games) // flattens to an ienumerable of games
    .Where(g => g.Name == gameName) // filters by the game name
    .OrderBy(g => g.Version) // orders by the game's version
    .ToList(); // converts the ienumerable to a list (of games).

如果你想要一个 List<RowData>相反,我们可以假设每行只有 1 个具有特定名称的游戏,您可以执行如下操作:

// delegate for generating orderby key    
Func<RowData, string, int> sortKey = (r, gn) =>
{
    var game = r.Games.FirstOrDefault(g => g.Name == gn);
    return game != null ? game.Version : int.MaxValue; // or "zzzzz" if version is a string (something to put it to the end of the list)
};

List<RowData> result = rows
    .OrderBy(r => sortKey(r, gameName))
    .ToList();

关于c# - 根据内部列表使用 .notation 对对象列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5317544/

相关文章:

c# - 如何发出 HTTP PUT 请求?

c# - 如何获取下个月的天数?

c# - 在一个语句中取消引用和提前指针?

c - 对二维数组进行排序,使最大的元素位于主对角线上

PHP/SQL : Sorting

c# - 使用 C# 查询 MongoDB 嵌套数组文档

string - 将后缀按后缀数组排序有何意义?

linq - 如何加入两个实体

c# - 如何使用 LINQ 或响应式(Reactive)扩展拆分带有嵌入转换标记的列表?

c# - LINQ To Object 是否在内部进行任何调整?