gpt4 book ai didi

C# LINQ GroupJoin 跳过项目

转载 作者:太空宇宙 更新时间:2023-11-03 15:34:26 26 4
gpt4 key购买 nike

我有以下代码

  var profilesWithMailLists = profilesWithCf.GroupJoin(
profile2Maillist,
p => p.SqlId,
p2l => p2l.ProfileId,
(p, mailLists) =>
{
var p2lmongo = mailLists.Select<TProfile2MailList, Profile2MailList>(p2l =>
{
return new Profile2MailList
{
MailListId = p2l.MailListId,
Status = p2l.Status,
SubscriptionDate = p2l.SubscriptionDate
};
});
p.MailLists = p2lmongo.ToArray();
return p;
});

如果 p2lmongo 集合将包含 0 个元素,是否可以跳过迭代并返回配置文件?

最佳答案

尝试使用 GroupByJoin 而不是 GroupJoin

一些代码来说明(未经测试或类型检查):

var profilesWithMailListsAlt =
profilesWithCf.Join(
profile2Maillist.GroupBy(p2l => p2l.ProfileId),
p => p.SqlId,
p2lgroup => p2lgroup.Key,
(p, mailListGroup) =>
{
var p2lmongo = mailListGroup.Select<TProfile2MailList, Profile2MailList>(p2l =>
{
return new Profile2MailList
{
MailListId = p2l.MailListId,
Status = p2l.Status,
SubscriptionDate = p2l.SubscriptionDate
};
});
p.MailLists = p2lmongo.ToArray();
return p;
});

如果仍然不能满足您的要求,请尝试通过更改

明确排除空组
profile2Maillist.GroupBy(p2l => p2l.ProfileId)

profile2Maillist.GroupBy(p2l => p2l.ProfileId).Where(group => group.Any())

这里只是从臀部射击。

关于C# LINQ GroupJoin 跳过项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32332199/

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