c# - 如何在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 的情况下完成这项工作,如果我以错误的方式使用它或我可以做任何改进,请建议我。
谢谢
解决方案
您可以按以下方式分组并对每个组进行fileVersionDetails
排序:Id
Version
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();
推荐阅读
- amazon-web-services - 亚马逊如何使用域名在 ACM 中颁发 SSL 证书
- php - 检索邮递员在php中提交的json
- unity3d - 如何统一显示2个形状的相交边?
- java - 透明背景上的 setColor 未显示正确的颜色
- python - 条形图中的动态阈值线
- python - 为什么 lambda x: x % 2 表示奇数
- reactjs - 如何添加 favicon 以响应应用程序浏览器选项卡(重复)
- r-markdown - 更改 kableExtra 的 PDF 输出中的字体系列
- java - 如何在 Netbeans 8.2 中使用 XSD 1.1?
- node.js - NodeJs 和 MongoDB