开发者

LINQ where query

开发者 https://www.devze.com 2023-01-29 19:29 出处:网络
Tables => Groups Subgroups I need to select ONLY groups from Groups table that have been checked via(checkboxes) which contain SubgroupId

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消