Reputation: 1249
I have created two LINQ queries to extract same type of value, but basing on different references in database.
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?
Upvotes: 1
Views: 106
Reputation: 1249
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
user.OrganizationalUnit.SelectMany(x => x.ProductGroup)
.Concat(user.SubOrganizationalUnit.SelectMany(x => x.ProductGroup))
.Select(x => x.ProductGroupId)
.Distinct()
.ToList()
Upvotes: 1
Reputation: 22886
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();
Upvotes: 1
Reputation: 759
how about you Concat only a part of both queries: the first part, since the 2nd parts are identical?
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)
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;
}
Upvotes: 1
Reputation: 156728
As long as they have the exact same structure, you should be able to use Concat()
, which translates to a UNION ALL
in SQL.
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();
Upvotes: 4