【问题标题】:How do I get projection of one list to another with Linq? [duplicate]如何使用 Linq 将一个列表投影到另一个列表? [复制]
【发布时间】:2018-12-10 20:43:07
【问题描述】:

有两个类:

public class Persons
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string LastName { get; set; }
    public int Phone { get; set; }
    public DateTime Time { get; set; }
}

public class Phones
{
    public string Name { get; set; }
    public int Phone { get; set; }
}

我需要使用 linq 获取 PhoneList 的 Persons 投影:

List<Persons> personList = List<Persons>{...};

List<PhoneList> phoneList = persons...

我如何获得它?

【问题讨论】:

  • 所以您基本上从未接触过 LinQ,而不是搜索教程,您认为问一个问题会更好吗?还是我们缺少什么?

标签: c# list linq dictionary projection


【解决方案1】:

使用Select 子句进行投影:

List<PhoneList> phoneList = personList.Select(x => new Phones { Name = x.Name, Phone = x.Phone }).ToList();

【讨论】:

    猜你喜欢
    • 2014-10-05
    • 1970-01-01
    • 2017-11-02
    • 2016-02-28
    • 1970-01-01
    • 2012-07-21
    • 2019-01-26
    • 2021-08-30
    • 2022-11-12
    相关资源
    最近更新 更多