Search code examples
c#databaselinqjoinef-core-3.1

How to apply joins on different context files


var InsertionId = 
    _DvContext.DvInsertionOrder
        .Select(x => new { x.Id, x.KiteDealId, x.KiteCampaignId, x.CampaignId })
        .ToList();

var camapignId = _kcdbContext.KiteLineitems
                 .Select(x=> new { x.CampaignId, x.Id }).ToList(); 

I have these two queries how I apply join on it. In where condition I want to match campaignID.


Solution

  • You cannot join two dbcontext on one request. You must store them in memory.

    By executing ToList(), the EF/SQL query is executed and it is now in memory. So you could try now something like this

    
    //implement your where and select, etc.
    var objectsFromContext1 = _dbContext1.Entities.(where,select to DTOObject1).ToList();
    
    //implement your where and select, etc.
    var objectsFromContext2 = _dbContext2.Entities.(where,select to DTOObject2).ToList();
    
    var joined = objectsFromContext1.Join(objectsFromContext2 ,
                          db1 => db1.PropertyToBeBasisOfJoined, 
                          db2 => db2.PropertyToBeBasisOfJoined, 
                          (db1 , db2 ) => new MyJoinedClass
                          {
                             //properties
                          }).ToList();