3

I have not found any good material for querying multiple databases using LINQ. I have been using connection strings to change between databases and display data for user, now i want to implement a search function which query's all databases and returns a list instead of selecting the database before hand.

Here's what I've thrown together so far. which returns one list from one database which obviously is not what i want.

 public ActionResult getCustomers(string cust)
    {
        List<trakman_Entities> teInstances = new List<trakman_Entities>();
        IEnumerable<customer> customers = null;


        for (var i = 1; i < ConfigurationManager.ConnectionStrings.Count; i++)
        {

            if (ConfigurationManager.ConnectionStrings[i].ConnectionString.ToLower().Contains("metadata"))
            {
                string con = ConfigurationManager.ConnectionStrings[i].ConnectionString;
                teInstances.Add(new trakman_Entities(con));

            }

        }

        foreach (trakman_Entities entitiy in teInstances)
        {
            customers = entitiy.customers.Where(c => c.code.StartsWith(cust));

        }

        foreach(customer c in customers)
        {
            Response.Write(c.code);
            Response.Write(c.name);

        }

        Response.End();
        return View(customers);
    }

1 Answer 1

1

The problem is that you keep reassigning the customers variable in your foreach loop:

    foreach (trakman_Entities entitiy in teInstances)
    {
        // overwrites on each iteration!
        customers = entitiy.customers.Where(c => c.code.StartsWith(cust));

    }

Instead, consider:

var customers = teInstances.SelectMany(e => e.customers.Where(c => c.code.StartsWith(cust))) 
    .ToList();

Or, do do the whole thing using a single LINQ query:

// start with the list of connection string settings cast to IEnumerable<T>
var customers = ConfigurationManager.ConnectionStrings.Cast<ConnectionStringSettings>()
    // filter to the relevant connection strings
    .Where(s => s.ConnectionString.ToLower().Contains("metadata"))
    .SelectMany(s => {
         // for each connection string, select a data context        
         using( var context = new trakman_entities(s.ConnectionString)) {
             // for each context, select all relevant customers
             return context.customers.Where(c => c.code.StartsWith(cust)).ToArray();
         } // and dispose of the context when we're done with it
     })
    .ToList();
Sign up to request clarification or add additional context in comments.

4 Comments

I know lol, Im a bit stuck on implementing a list containing a list of customers.
@Simon Did you try my suggestion?
@ChaseMedallion better customers.Add(...) where customers is List
That is bringing back what looks to be right, just need to check results.

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.