2

I am using results from Predicate query to write join in Linq query. But it is throwing the following error. However when I run similar query in SQL, it returns the expected results.

var predicate = PredicateBuilder.False<Product>();
foreach (var productId in productIds)
{
    var tempGuid = productId;
    predicate = predicate.Or(p => p.ProductId== tempGuid.ToString());
}

// This query is returning products back
var products = from p in context.ProductSet.AsExpandable().Where(predicate)
                           select p;

var accounts = (from a in context.AccountSet
                join cl in context.ContractDetailSet 
                on a.AccountId equals cl.AccountId.Id
                join p in products on  \\ From predicate query
                cl.ProductId.Id equals p.ProductId
                where a.StateCode == AccountState.Active                    
                select a).ToList();

Note: I have removed the ToList() in the end and it does not throw the error then, but when you try to use the accounts object then it throws the same error again.

Error

An exception of type 'System.NullReferenceException' occurred in Microsoft.Xrm.Sdk.dll but was not handled in user code.

Additional information: Object reference not set to an instance of an object.

Daryl
  • 18,592
  • 9
  • 78
  • 145
user1211185
  • 731
  • 3
  • 12
  • 27
  • var products = (from p in context.ProductSet.AsExpandable().Where(predicate) select p).ToList(); Are query above works? – Oleg Jun 12 '15 at 09:29
  • No, it timesout now. – user1211185 Jun 12 '15 at 09:45
  • If you code is working fine till Predicate, I would recommend you to use query expression then. Run foreach on products and add `ConditionExpression` inside foreach. Also, I think QueryExpression are quicker than Linq. – Scorpion Jun 12 '15 at 13:38
  • @Scorpion I am not very familiar with Query Expression. Can you please provide an example. – user1211185 Jun 12 '15 at 14:20

1 Answers1

2

Following works for me perfectly, Replace Linq with QueryExpression.

var qe = new QueryExpression {EntityName = "account", ColumnSet = new ColumnSet()};
qe.ColumnSet.Columns.Add("name");

qe.LinkEntities.Add(new LinkEntity("account", "contractdetail", "accountid", "accountid", JoinOperator.Inner));
qe.LinkEntities[0].Columns.AddColumns("productid", "title");
qe.LinkEntities[0].EntityAlias = "contractdetails";

// Check Account State
FilterExpression accountState = qe.Criteria.AddFilter(LogicalOperator.And);
accountState.Conditions.Add(new ConditionExpression("statecode", ConditionOperator.Equal, 0));

FilterExpression productFilter = qe.LinkEntities[0].LinkCriteria.AddFilter(LogicalOperator.Or);
foreach (var product in products)
{
    var condition = new ConditionExpression
    {
        AttributeName = "productid",
        Operator = ConditionOperator.Equal
    };
    condition.Values.Add(product.ProductId);
    productFilter.Conditions.Add(condition); 
}                

EntityCollection resultsCollection = _OrgService.RetrieveMultiple(qe);

For more details about QueryExpression check the links below.

  1. Retrieve multiple with the QueryExpression
  2. Use the ConditionExpression
Scorpion
  • 4,495
  • 7
  • 39
  • 60