Home > Net >  How to union /join multiple Db Context?
How to union /join multiple Db Context?

Time:12-27

How to union two multiple dbcontext, We have two dbcontext here, that have the same properties and need to join both DB context.Both schemes are present in the same database.

public class Employee
{
    public string employeeid { get; set; }
    public string Name { get; set; }
    public string FullName { get; set; }
    public string MobileNumber { get; set; }
    public string Age { get; set; }
}

public class EmployeeService
{
    private readonly IEmployee _employeeserice;
    private readonly IEmployeeArchive _employeearchiveService;

    public EmployeeService(IEmployee employeeservice, IEmployeeArchive employeearchiveService)
    {
        _employeeserice = employeeservice;
        _employeearchiveService = employeearchiveService;

    }
}


    public List<Employee> Resuts(int employeeid)
    {
        var employee = _employeeserice.Queryable().Where(q => q.employeeid == employeeid);
        var employeearchive = _employeearchiveService.Queryable().Where(q => q.employeeid == employeeid);
        return employee.contact(employeearchive);
    }

union of two dbcontext results

CodePudding user response:

If both contexts are in the same database, then consider NOT using different contexts, and just do the join within a single context.

If they are separate databases, what you need to do is independently query each database, then join the results in memory (rather in the database engine).

For example:

var foos = contextA.Foos.Where(foo => foo.Field > 10).ToArray();
var bars = contextB.Bars.Where(bar => bar.AnotherField == "something").ToList();


var join = foos.Join(
  bars,
  inner => inner.ForeignKey,
  outer => outer.Key,
  (inner,outer) => new {
    Foo = inner,
    Bar = outer
  }
);

In the above example, the ToArray and ToList methods both concretize the query, and the respective databases for each context are queried and the results are returned and read into the program memory.

Both collections are then joined in memory.

Because the join is occurring in memory, you should limit the number of query results by appropriately filtering. Additionally you can use the Select operation to select only the fields you are interested in.

If you want to do different type of joins, take a look at the GroupJoin operation which can allow you to do outer joins.

CodePudding user response:

Assuming from the signature you want to return List and do not want to have cross-context query, then something like the following should do (using AsEnumerable to switch from SQL generation to client side processing):

public List<Employee> Resuts(int employeeid)
{
    var employee = _employeeserice.Queryable().Where(q => q.employeeid == employeeid);
    var employeearchive = _employeearchiveService.Queryable().Where(q => q.employeeid == employeeid);
    
    return employee.AsEnumerable().Contact(employeearchive).ToList();
}

Also maybe you should consider using async queries.

  • Related