ShaneKm
ShaneKm

Reputation: 21308

LINQ where query

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<campaignSegmentCondition>();

Upvotes: 0

Views: 116

Answers (2)

Enigmativity
Enigmativity

Reputation: 117029

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))

Upvotes: 1

Unmesh Kondolikar
Unmesh Kondolikar

Reputation: 9312

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.

Upvotes: 0

Related Questions