C#C
C#3y ago
SWEETPONY

❔ How to rewrite this without second foreach?

static private IEnumerable<string> GetSubjectsCustomFields(
        IEnumerable<(string Id, string TypeId, CustomFieldsValueDto CustomFields)> subjects,
        IReadOnlySet<string> subjectIds,
        IEnumerable<ChannelSettingsDto> channelSettings)
    {
        var join = subjects
            .Where(subject => subjectIds.Contains(subject.Id))
            .Join(channelSettings,
                subject => subject.TypeId,
                setting => setting.SubjectType,
                (subject, setting) => (subject, setting));
        
        foreach (var tuple in join)
        {
            if (tuple.subject.CustomFields?.FieldValues != null
                 && tuple.subject.CustomFields.FieldValues
                     .TryGetValue( tuple.setting.CustomField, out var value))
            {
                yield return value.ToString();
            }
        }
    }
Was this page helpful?