gpt4 book ai didi

c# - 保存到元组的逻辑问题

转载 作者:太空宇宙 更新时间:2023-11-03 22:42:05 25 4
gpt4 key购买 nike

我有一个 List<Person> , 其中Person对象由以下元素组成;

Id

Name

Age

School

我要查询这个List并保存IdAgeList<int,int> .

我知道 List<int,int>不能用 C# 完成,所以我发现 Tuple<List<int,int>>可以代替使用。

List<Person> personList = _db.Persons.OrderBy(x=> x.Id == Id).ToList();
Tuple<List<int,int>> listOfIdandAge = personList.Select(r => r.Id).ToList();

上面的代码不起作用,因为我无法保存 AgelistOfIdandAge列表。有人可以帮我解决这个问题吗?

最佳答案

List<(int, int)> listOfIdAndAge = personList.Select(r => (r.Id, r.Age)).ToList();

或者实际上,这是 C#,所以

var listOfIdAndAge = personList.Select(r => (r.Id, r.Age)).ToList();

为了方便访问:

var listOfIdAndAge = personList.Select(r => (id: r.Id, age: r.Age)).ToList();

var firstId = listOfIdAndAge.First().id;
var firstAge = listOfIdAndAge.First().age;

关于c# - 保存到元组的逻辑问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51710838/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com