简体   繁体   English

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

[英]Merge two LINQ expressions into one

I have created two LINQ queries to extract same type of value, but basing on different references in database. 我创建了两个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);

Is there any way to connect them into one LINQ query? 有什么方法可以将它们连接到一个LINQ查询中吗?

As long as they have the exact same structure, you should be able to use Concat() , which translates to a UNION ALL in SQL. 只要它们具有完全相同的结构,就应该能够使用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();

how about you Concat only a part of both queries: the first part, since the 2nd parts are identical? 您如何看待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;

or see expanded example where combinedProductGroups are a separate variable (that's 1st part of your query combined for both lists) 或查看其中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;
}

If the tables have the same structure, I am guessing you might be able to 如果表具有相同的结构,我想您也许可以

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

Based on all of your answers I made this 根据您的所有回答,我做到了

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

I have also created this using Lambda 我也使用Lambda创建了它

user.OrganizationalUnit.SelectMany(x => x.ProductGroup)
    .Concat(user.SubOrganizationalUnit.SelectMany(x => x.ProductGroup))
    .Select(x => x.ProductGroupId)
    .Distinct()
    .ToList()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM