gpt4 book ai didi

c# 使用 Linq 将数组中的数组投影到新数组中?

转载 作者:行者123 更新时间:2023-12-04 09:43:58 25 4
gpt4 key购买 nike

我正在使用 .NET Core 并构建 API。我有传入的 JSON,我将其映射到 C# 对象,并且在大多数情况下,JSON 非常简单,除了一个可能在数组中包含数组的部分,请参见下文:

"marketing_preferences": [
{
"channel": "EDM",
"opt_in": false,
"valid_from_date": "2020-05-30T07:07:53.723Z",
"content_type_preferences": [
{
"type": "CAT",
"opt_in": false,
"valid_from_date": "2020-05-30T07:07:53.724Z"
},
{
"type": "TAC",
"opt_in": true,
"valid_from_date": "2020-05-30T07:07:53.724Z"
}
]
},
{
"channel": "SMS",
"opt_in": true,
"valid_from_date": "2020-05-30T07:07:53.724Z",
"content_type_preferences": []
},
{
"channel": "SM",
"opt_in": true,
"valid_from_date": "2020-05-30T07:07:53.724Z",
"content_type_preferences": []
}
]

营销偏好可能(但可能不)包含一个或多个 content_type_preferences。因此,marketing_preferences 是一个数组,而 content_type_preferences 是该数组中的一个可选数组。在我的代码中映射或投影营销偏好非常简单:
    customer.MarketingPreferences = source.RequestCustomer.MarketingPreferences
.Select(x => new MarketingPreferences()
{
ChannelId = x.Channel,
OptIn = x.OptIn,
ValidFromDate = x.ValidFromDate,
UpdatedBy = Functions.DbUser,
CreatedDate = DateTime.UtcNow,
UpdatedDate = DateTime.UtcNow,
UpdatingStore = null // Not passed by API
})
.ToList();

但是,我对将 content_type_preferences 投影到其等效对象上的语法有些困惑。以下是我试图转换到的类(class):
public partial class MarketingPreferences
{
public MarketingPreferences()
{
ContentTypePreferences = new HashSet<ContentTypePreferences>();
}

public Guid CustomerInternalId { get; set; }
public string ChannelId { get; set; }
public bool? OptIn { get; set; }
public DateTime? ValidFromDate { get; set; }
public string UpdatedBy { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? UpdatedDate { get; set; }
public string UpdatingStore { get; set; }

public virtual Customer CustomerInternal { get; set; }
public virtual ICollection<ContentTypePreferences> ContentTypePreferences { get; set; }
}


public partial class ContentTypePreferences
{
public Guid CustomerInternalId { get; set; }
public string ChannelId { get; set; }
public string TypeId { get; set; }
public bool? OptIn { get; set; }
public DateTime? CreatedDate { get; set; }
public DateTime? UpdatedDate { get; set; }
public DateTime? ValidFromDate { get; set; }
public string UpdatedBy { get; set; }
public string UpdatingStore { get; set; }

public virtual MarketingPreferences C { get; set; }
public virtual Customer CustomerInternal { get; set; }
}

我确实开始恢复到笨拙的方式来做这件事,但我真的不满意,并且更愿意尝试找到 LINQ-ish 方式来实现以下目标:
    foreach (var marketingPrefs in source.RequestCustomer.MarketingPreferences)
{
if (marketingPrefs.ContentTypePreferences.Length > 0)
{
Console.WriteLine($"marketing preference: {marketingPrefs.Channel}");
foreach (var contentPrefs in marketingPrefs.ContentTypePreferences)
{
Console.WriteLine($"content preference: {contentPrefs.Type}");
customer.ContentTypePreferences.Add(new ContentTypePreferences
{
TypeId = contentPrefs.Type,
OptIn = contentPrefs.OptIn,
ValidFromDate = contentPrefs.ValidFromDate
});
}
}
}

最佳答案

简单 SelectMany 应该足以压平集合:

customer.ContentTypePreferences = source.RequestCustomer.MarketingPreferences
.SelectMany(mp => mp.ContentTypePreferences)
.Select(contentPrefs => new ContentTypePreferences
{
TypeId = contentPrefs.Type,
OptIn = contentPrefs.OptIn,
ValidFromDate = contentPrefs.ValidFromDate
})
.ToList();

关于c# 使用 Linq 将数组中的数组投影到新数组中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62205746/

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