Tables => Groups Subgroups
I need to select ONLY groups from Groups table that have been checked via(checkboxes) which contain SubgroupId
Statement below works but it also selects groups that have empty subgroups. please help
var test =
this.MailingGroupRepository.List().ToList()
.Cast<MailingGroup>()
.Select(e => new campaignSegmentCondition
{
extra = string.Empty,
field = "interests-" + e.GroupingId,
op = "all",
value = string.Join(",",
updateRow.MailingSubgroup
.Where(r => r.MailingGroupId == e.MailingGroupId)
.Select(p => p.Name))
}).ToList&开发者_StackOverflowlt;campaignSegmentCondition>();
If I understand the problem, then there is a very easy solution. Try this:
var test =
this.MailingGroupRepository.List().ToList()
.Cast<MailingGroup>()
.Where(e => updateRow.MailingSubgroup
.Any(r => r.MailingGroupId == e.MailingGroupId))
.Select(e => new campaignSegmentCondition
{
extra = string.Empty,
field = "interests-" + e.GroupingId,
op = "all",
value = string.Join(",",
updateRow.MailingSubgroup
.Where(r => r.MailingGroupId == e.MailingGroupId)
.Select(p => p.Name))
}).ToList<campaignSegmentCondition>();
Note the inclusion of the following code:
.Where(e => updateRow.MailingSubgroup
.Any(r => r.MailingGroupId == e.MailingGroupId))
Can you check if a mailing subgroup is empty or not in you where clause? -
updateRow.MailingSubgroup
.Where(r => !r.IsEmpty && r.MailingGroupId == e.MailingGroupId)
if there is no IsEmpty
property you can also check something like r.Items.Count > 0
if possible.
精彩评论