将两个LINQ表达式合并为一个

时间:2017-02-07 22:11:28

标签: c# linq

我创建了两个LINQ查询来提取相同类型的值,但是基于数据库中的不同引用。

var productGroupIdList = (from organizationalUnit 
                          in user.OrganizationalUnit
                          from productGroup 
                          in organizationalUnit.ProductGroup
                          select productGroup.ProductGroupId).ToList();
productGroupIdList.AddRange(from subOrganizationalUnit 
                            in user.SubOrganizationalUnit
                            from productGroup 
                            in subOrganizationalUnit.ProductGroup
                            select productGroup.ProductGroupId);

有没有办法将它们连接到一个LINQ查询?

4 个答案:

答案 0 :(得分:4)

只要它们具有完全相同的结构,您就应该能够使用Concat(),这将转换为SQL中的UNION ALL

var productGroupIdList = (from organizationalUnit 
                      in user.OrganizationalUnit
                      from productGroup 
                      in organizationalUnit.ProductGroup
                      select productGroup.ProductGroupId)
                   .Concat(
                        from subOrganizationalUnit 
                        in user.SubOrganizationalUnit
                        from productGroup 
                        in subOrganizationalUnit.ProductGroup
                        select productGroup.ProductGroupId)
                   .ToList();

答案 1 :(得分:1)

你怎么样Concat只是两个查询的一部分:第一部分,因为第二部分是相同的?

var productGroupIdList = from productGroup in 
            (from organizationalUnit in user.OrganizationalUnit
            select organizationalUnit.ProductGroup)
            .Concat(
                from subOrganizationalUnit 
                in user.SubOrganizationalUnit
                select subOrganizationalUnit.ProductGroup)
        select productGroup.ProductGroupId;

或查看扩展示例,其中combinedProductGroups是一个单独的变量(这是两个列表的查询的第一部分)

class Program
{
    static void Main(string[] args)
    {
        var user = new User();

        var combinedProductGroups = (from organizationalUnit
                in user.OrganizationalUnit
                select organizationalUnit.ProductGroup)
            .Concat(from subOrganizationalUnit
                in user.SubOrganizationalUnit
                select subOrganizationalUnit.ProductGroup);

        var productGroupIdList = from productGroup in combinedProductGroups
                select productGroup.ProductGroupId;

    }
}

public class User
{
    public List<OrgUnit> OrganizationalUnit;
    public List<SubOrgUnit> SubOrganizationalUnit;
}

public class OrgUnit
{
    public ProdGroup ProductGroup;
}

public class SubOrgUnit
{
    public ProdGroup ProductGroup;
}

public class ProdGroup
{
    public string ProductGroupId;
}

答案 2 :(得分:1)

如果表格具有相同的结构,我猜你可能

var result = user.OrganizationalUnit.SelectMany(x => x.ProductGroup.ProductGroupId)
  .Concat(user.SubOrganizationalUnit.SelectMany(x => x.ProductGroup.ProductGroupId)).ToList();

答案 3 :(得分:1)

根据你提出的所有答案

var productGroupIdList = (from organizationalUnit 
                          in user.OrganizationalUnit
                          from subOrganizationalUnit
                          in user.SubOrganizationalUnit
                          from productGroup 
                          in organizationalUnit.ProductGroup.Concat(subOrganizationalUnit.ProductGroup)
                          select productGroup.ProductGroupId).Distinct().ToList();

我也使用Lambda

创建了这个
user.OrganizationalUnit.SelectMany(x => x.ProductGroup)
    .Concat(user.SubOrganizationalUnit.SelectMany(x => x.ProductGroup))
    .Select(x => x.ProductGroupId)
    .Distinct()
    .ToList()