0

I'm trying to do this code:

    public ActionResult JoinSupToPro()
    {

        SupplierDBContext dbS = new SupplierDBContext();
        var innerJoinQuery = from pro in db.Products join sup in dbS.Suppliers on pro.SupplierId equals sup.ID
        select new {Name= pro.Name,Price =pro.Price, SupplierName =sup.Name , Phone =sup.Phone};
        IndexModel m = new IndexModel();
        foreach (var item in innerJoinQuery)
        {
            SupplierProduct p = new SupplierProduct();
            p.SupplierName = item.SupplierName;
            p.Phone = item.Phone;
            p.Price = item.Price;
            p.ProductName = item.Name;
                m.MenuItems.Add(p);
        }

        return View(m.MenuItems.ToList());
    }

I'm getting this problem : The specified LINQ expression contains references to queries that are associated with different contexts. Any ideas?

4
  • Which line is throwing the error? Commented Jul 1, 2015 at 15:14
  • MenuItems is a list of products in class IndexModel : i need that to list so i can later show them in the view Commented Jul 1, 2015 at 15:14
  • Try add ToList() at the end of your first query Commented Jul 1, 2015 at 15:22
  • see this if it helps : stackoverflow.com/questions/898363/… Commented Jul 1, 2015 at 15:22

2 Answers 2

3

It looks like you are joining data from two different contexts: tms and entities.

This is not possible in LINQ as both have their own connection to the database and a completely separate model.

It's not possible for EF to convert this into a SQL statement. (for all it knows, the tables might live in a different database)

You'd need to either move all your entities to a single context or execute both queries separately and then join them in memory. (use the first option if all tables are in the same DB, use the second if you have separate databases)

Sign up to request clarification or add additional context in comments.

2 Comments

I need the second option i dont see 2 queries here can you explain?
It seems you have two dbcontext (db & dbs). merge them.
0

While working I found that the solution to my answers is to separate the 2 queries and then join between them like so:

    public ActionResult JoinSupToPro()
    {

        List<Supplier> dbS = new SupplierDBContext().Suppliers.ToList();
        List<Product> prod = db.Products.ToList();
        var innerJoinQuery = from pro in prod join sup in dbS on pro.SupplierId equals sup.ID
        select new {Name= pro.Name,Price =pro.Price, SupplierName =sup.Name , Phone =sup.Phone};
        IndexModel m = new IndexModel();
        m.MenuItems = new List<SupplierProduct>();
        foreach (var item in innerJoinQuery)
        {
            SupplierProduct p = new SupplierProduct();
            p.SupplierName = item.SupplierName;
            p.Phone = item.Phone;
            p.Price = item.Price;
            p.ProductName = item.Name;
                m.MenuItems.Add(p);
        }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.