LINQ在哪里查询

时间:2010-12-07 08:50:34

标签: .net linq linq-to-sql

Tables => 组 子群

我需要从Groups表中选择仅通过(复选框)检查的组包含SubgroupId

下面的语句有效,但它也会选择具有空子组的组。请帮忙

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

2 个答案:

答案 0 :(得分:1)

如果我理解这个问题,那么有一个非常简单的解决方案。试试这个:

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

请注意包含以下代码:

        .Where(e => updateRow.MailingSubgroup
                    .Any(r => r.MailingGroupId == e.MailingGroupId))

答案 1 :(得分:0)

您可以在where where子句中检查邮件子组是否为空? -

updateRow.MailingSubgroup
   .Where(r => !r.IsEmpty && r.MailingGroupId == e.MailingGroupId)

如果没有IsEmpty属性,您还可以检查r.Items.Count > 0之类的内容。