C
C#13mo 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();
}
}
}
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();
}
}
}
11 Replies
SWEETPONY
SWEETPONYOP13mo ago
I'd like to have only one linq query
Angius
Angius13mo ago
.Select()?
SWEETPONY
SWEETPONYOP13mo ago
but I can't write if statement in select
Angius
Angius13mo ago
Sure you can
SWEETPONY
SWEETPONYOP13mo ago
var join = subjects
.Where(subject => subjectIds.Contains(subject.Id))
.Join(channelSettings,
subject => subject.TypeId,
setting => setting.SubjectType,
(subject, setting) => (subject, setting))
.Select(tuple =>
{
if (tuple.subject.CustomFields?.FieldValues != null
&& tuple.subject.CustomFields.FieldValues
.TryGetValue(tuple.setting.CustomField, out var value))
{
return value.ToString();
}
} );

var join = subjects
.Where(subject => subjectIds.Contains(subject.Id))
.Join(channelSettings,
subject => subject.TypeId,
setting => setting.SubjectType,
(subject, setting) => (subject, setting))
.Select(tuple =>
{
if (tuple.subject.CustomFields?.FieldValues != null
&& tuple.subject.CustomFields.FieldValues
.TryGetValue(tuple.setting.CustomField, out var value))
{
return value.ToString();
}
} );

this is a little bit bad because I should return smth from select
Angius
Angius13mo ago
Yeah, you would need an else And then .Where() to filter out the empty values
SWEETPONY
SWEETPONYOP13mo ago
but actually I don't want to return smth on else
Angius
Angius13mo ago
.Aggregate() could also work I guess
SWEETPONY
SWEETPONYOP13mo ago
hm
Omnissiah
Omnissiah13mo ago
maybe you can filter something first with .Where() to simplify the if in a more linear way but even without it simply returning null as Z says would be good
Accord
Accord13mo ago
Was this issue resolved? If so, run /close - otherwise I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server