gpt4 book ai didi

c# - 作为可投影对象的类和结构

转载 作者:太空宇宙 更新时间:2023-11-03 10:52:48 24 4
gpt4 key购买 nike

很多时候我必须从一个类中选择所需的字段,就像在 sql 中从表中选择所需的字段一样。所以我正在考虑将我的类和结构作为 Projectables。

这是我想要实现的。

public class Email
{
public string Subject { get; set; }
public string To { get; set; }
public string Cc { get; set; }
public string Bcc { get; set; }
public string Body { get; set; }
public DateTime ReceivedDate { get; set; }
}

public class EmailProjection
{
// instead of this

//public string Subject { get; set; }
//public DateTime ReceivedDate { get; set; }

// I want to do this
Email.Project(Subject,ReceivedDate);
}

public class EntryPoint
{
static void Main()
{
var emailList = new List<Email>();
/* populate emails here from source*/

// I want to do this
List<EmailProjection> emailProjectionList = emailList.Project(EmailProjection).ToList();

// rather than
List<EmailProjection> emailProjectionList= emailList.Select(email=>new {email.Subject,email.ReceivedDate}).ToList();}
}

有人可以帮我解决这个问题吗?提示??它可以实现吗?

最佳答案

你可以看看Automapper:https://github.com/AutoMapper/AutoMapper .它是一个基于命名约定和规则映射 DTO 的库。看看维基有一个 Projection页,那可能就是您要找的那个。只是为了连接您的示例,您最终可以编写如下代码:

Mapper.Map<EMail, EmailProjection>(yourObjects);

关于c# - 作为可投影对象的类和结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20775354/

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