0

I can't to query a collection like this below, in order to get the maximun "value" of a given "key", but key and value are subcollection of a collection

I need to get the max value of the "Revision" key of this collection:

- 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"

any hints?

1
  • 1
    can you share some code, like classes, document list initialization...? Commented May 7, 2020 at 13:19

1 Answer 1

0

Using the linq Zip function allows you to combine these two sub arrays into one. The following is an example of getting the maximum revision for each version:

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" }
        }};
}

This will output the following:

Version: 1.0 - MaxRevision: 3
Version: 2.0 - MaxRevision: 1
Sign up to request clarification or add additional context in comments.

1 Comment

If this answer meets the requirements of a solution, then please mark it as such. If not, then please let me know how I can help, I'll be happy to expand on the above to get you the solution you're looking for.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.