Below is the code that I have which basically trying do a union for two IQueryable but I am not getting any results back from the second IQueryable. Am I missing something.
public IQueryable<SalesOrder> GetSalesOrder(Guid contactId, string memberId)
{
var crmContext = new Xrm.XrmServiceContext("Xrm");
var organizations = IsPrimaryContact(contactId);
var query = from a in crmContext.SalesOrderSet
where a.CustomerId.Id == contactId
where a.StateCode.Value == 0
where a.my_OrderStatus.Value == (int)SalesOrdermy_OrderStatus.New || a.my_OrderStatus.Value == (int)SalesOrdermy_OrderStatus.Shipped
select new SalesOrder
{
SalesOrderId = a.SalesOrderId.Value,
InvoiceNumber = a.OrderNumber,
MemberId = memberId,
BalanceDue = a.my_balancedue,
AmountPaid = a.TotalAmount,
OrderDate = a.my_OrderDate,
DueDate = a.my_DueDate,
CustomerType = "Individual",
BillToAddress1 = a.BillTo_Line1,
BillToAddress2 = a.BillTo_Line2,
BillToCity = a.BillTo_City,
BillToPostalCode = a.BillTo_PostalCode,
BillToState = a.BillTo_StateOrProvince,
BillToCountry = a.BillTo_Country,
OrderStatus = Utility.GetOptionSetValueLabel(crmContext, new Entity(Xrm.SalesOrder.EntityLogicalName), "my_orderstatus", new OptionSetValue(a.my_OrderStatus.Value))
};
foreach (var id in organizations)
{
var query1 = from a in crmContext.SalesOrderSet
join b in crmContext.AccountSet
on a.CustomerId.Id equals b.AccountId.Value
where a.StateCode.Value == 0
where a.my_OrderStatus.Value == (int)SalesOrdermy_OrderStatus.New || a.my_OrderStatus.Value == (int)SalesOrdermy_OrderStatus.Shipped
where b.PrimaryContactId.Id == contactId
select new SalesOrder
{
SalesOrderId = a.SalesOrderId.Value,
InvoiceNumber = a.OrderNumber,
MemberId = b.my_memberid,
BalanceDue = a.my_balancedue,
AmountPaid = a.TotalAmount,
OrderDate = a.my_OrderDate,
DueDate = a.my_DueDate,
CustomerType = "Organization",
BillToAddress1 = a.BillTo_Line1,
BillToAddress2 = a.BillTo_Line2,
BillToCity = a.BillTo_City,
BillToPostalCode = a.BillTo_PostalCode,
BillToState = a.BillTo_StateOrProvince,
BillToCountry = a.BillTo_Country,
OrderStatus = Utility.GetOptionSetValueLabel(crmContext, new Entity(Xrm.SalesOrder.EntityLogicalName), "my_orderstatus", new OptionSetValue(a.my_OrderStatus.Value))
};
query.Union(query1);
}
return query;
}