【问题标题】:query subcollection when key and value are different subcollections - linq当键和值是不同的子集合时查询子集合 - linq
【发布时间】:2020-08-22 18:13:49
【问题描述】:

我不能像下面这样查询集合,以获得给定“键”的最大“值”,但键和值是集合的子集合

我需要获取这个集合的“Revision”键的最大值:

- document 
    - id
    - description
    - key
        - version
        - revision
        - otherkey
    - value
        - "1.0"
        - 1
        - "othervalue"
- document 
    - id
    - description
    - key
        - version
        - revision
        - otherkey
    - value
        - "1.0"
        - 2
        - "othervalue"
- document 
    - id
    - description
    - key
        - version
        - revision
        - otherkey
    - value
        - "1.0"
        - 3
        - "othervalue"
- document 
    - id
    - description
    - key
        - version
        - revision
        - otherkey
    - value
        - "2.0"
        - 1
        - "othervalue"

有什么提示吗?

【问题讨论】:

  • 能否分享一些代码,比如类,文档列表初始化...?

标签: c# linq linq-to-objects


【解决方案1】:

使用 linq Zip 函数可以将这两个子数组合并为一个。以下是获取每个版本的最大修订的示例:

void Main()
{
    var data = GetData();

    var maxRevisions = data
        .Select(x => new { Document = x, KeyValue = x.Key.Zip(x.Value, (k, v) => new { Key = k, Value = v }) })
        .GroupBy(x => new { Version = x.KeyValue.First(kv => kv.Key == "version").Value })
        .Select(x => new { Version = x.Key.Version, MaxRevision = x.Max(y => y.KeyValue.First(kv => kv.Key == "revision").Value) })
        .ToList();


    maxRevisions.ForEach(mr => Console.WriteLine($"Version: {mr.Version} - MaxRevision: {mr.MaxRevision}"));
}

public class Document
{
    public int Id { get; set; }
    public string Description { get; set; }
    public List<String> Key { get; set; }
    public List<String> Value { get; set; }
}

public static List<Document> GetData()
{
    return new List<Document> {
        new Document {
            Id = 1,
            Description = "one-one",
            Key = new List<string> { "version", "revision", "otherkey" },
            Value = new List<string> { "1.0", "1", "othervalue" }
        },
        new Document {
            Id = 1,
            Description = "one-two",
            Key = new List<string> { "version", "revision", "otherkey" },
            Value = new List<string> { "1.0", "2", "othervalue" }
        },
        new Document {
            Id = 1,
            Description = "one-three",
            Key = new List<string> { "version", "revision", "otherkey" },
            Value = new List<string> { "1.0", "3", "othervalue" }
        },
        new Document {
            Id = 1,
            Description = "two-one",
            Key = new List<string> { "version", "revision", "otherkey" },
            Value = new List<string> { "2.0", "1", "othervalue" }
        }};
}

这将输出以下内容:

Version: 1.0 - MaxRevision: 3
Version: 2.0 - MaxRevision: 1

【讨论】:

  • 如果这个答案符合解决方案的要求,那么请标记为这样。如果没有,请告诉我如何提供帮助,我很乐意扩展上述内容,为您提供所需的解决方案。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-03-27
  • 1970-01-01
  • 2012-08-02
  • 1970-01-01
相关资源
最近更新 更多