首页 > 解决方案 > 如何在c#中使用带有条件的selectman

问题描述

我有文件搜索库,文件详细信息存储在 CosmosDB 中,结构如下

{​
  "id": "e7f56bbc-3387-4034-9eab-03cdaaa7bead",​
  "fileName": "reviewforq1",​
  "parentId": "",​
  "year": 2019,​
  "quarter": "q1",​
  "path": "<>",​
  "content": [​
    {​
      "version": 1,​
      "state": "submitted",​
      "createdBy": "person1",​
      "createdOn": "10/04/2019 15:16:00",​
      "data": [​
        {​
          "field": "scheme",​
          "value": "abc",​
          "isColorEnabled": "false",​
          "color": "",​
          "isFlagEnabled": "false",​
          "flags": [],​
          "isCommentsEnabled": "false",​
          "comments": ""​
        },​
        {​
          "field": "projectNumber",​
          "value": "abc123",​
          "isColorEnabled": "false",​
          "color": "",​
          "isFlagEnabled": "false",​
          "flags": [],​
          "isCommentsEnabled": "false",​
          "comments": ""​
        }
    }
}

我在库中使用下面的模型

public class Files
{
    [JsonProperty(PropertyName = "id")]
    public Guid Id { get; set; }
    [JsonProperty(PropertyName = "fileName")]
    public string FileName { get; set; }
    [JsonProperty(PropertyName = "parentId")]
    public Guid ParentId { get; set; }
    [JsonProperty(PropertyName = "year")]
    public int Year { get; set; }
    [JsonProperty(PropertyName = "quarter")]
    public string Quarter { get; set; }
    [JsonProperty(PropertyName = "path")]
    public string Path { get; set; }
    [JsonProperty(PropertyName = "content")]
    public List<Content> Content { get; set; }
}
public class FileVersionDetails
{
    public Guid Id { get; set; }
    public string Path { get; set; }
    public string Quarter { get; set; }
    public int Year { get; set; }
    public string FileName { get; set; }
    public List<Data> Data { get; set; }
    public string CreatedBy { get; set; }
    public DateTime CreatedOn { get; set; }
    public int Version { get; set; }
}

这就是我搜索文件集合以获取文件的最新版本的方式。

List<FileVersionDetails> latestFileVersionDetails = new List<FileVersionDetails>();
var fileVersionDetails = files
.SelectMany(j => j.Content,
    (parent, child) => new FileVersionDetails
    {
        Id = parent.Id,
        Path = parent.Path,
        Quarter = parent.Quarter,
        Year = parent.Year,
        FileName = parent.FileName,
        Data = child.Data,
        CreatedBy = child.CreatedBy,
        CreatedOn = child.CreatedOn,
        Version = child.Version
    });

var fileIds = files.Select(i => i.Id);

foreach (var item in fileIds)
{
    var fileVersions = fileVersionDetails.Where(k => k.Id == item);
    int maxVersion = fileVersions.Max(l => l.Version);

    latestFileVersionDetails.Add(fileVersions.Where(o => o.Version == maxVersion).FirstOrDefault());
}

这可行,但我认为应该有一种有效的方法可以在没有 foreach 的情况下完成这项工作,如果我以错误的方式使用它或我可以做任何改进,请建议我。

谢谢

标签: c#jsonlinqlambda

解决方案


您可以按以下方式分组并对每个组进行fileVersionDetails排序:IdVersion

var fileVersionDetails = ...;

List<FileVersionDetails> latestFileVersionDetails = fileVersionDetails
    .GroupBy(x => x.Id)
    .Select(g => g.OrderByDescending(g => g.Version).First())
    .ToList();

或者:

List<FileVersionDetails> latestFileVersionDetails = files
    .SelectMany(...)
    .GroupBy(x => x.Id)
    .Select(g => g.OrderByDescending(g => g.Version).FirstOrDefault())
    .ToList();

推荐阅读