Steven Zack
Steven Zack

Reputation: 5104

linq query from two database

I have a linq query from two database, however, each time the program stops at the query point. I don't know how to debug linq using VS. Can someone help me figure it out what's wrong here? Thank you.

public List<Promotion> GetBroder(string source)
        {
            string _connString = ConfigurationManager.AppSettings["DB1"];
            PromotionDataContext dc = new PromotionDataContext(_connString);
            string connString = ConfigurationManager.AppSettings["DB2"];
            ReachDirectDataContext RDdc = new ReachDirectDataContext(connString);
            return (from b in RDdc.BrokerNos
                    from p in dc.Promotions
                    where p.Source == source && p.Broker == b.BrokerNo1
                    select new Promotion() {Code=p.Code,BrokerName=b.Name}).ToList<Promotion>();
        }

Upvotes: 2

Views: 8498

Answers (3)

Leons
Leons

Reputation: 2674

You can use the following to display the generated SQL for the Linq statement.

ReachDirectDataContext RDdc = new ReachDirectDataContext(connString);

RDdc.Log = Console.Out;

return (from b in RDdc.BrokerNos
        from p in dc.Promotions     
        where p.Source == source && p.Broker == b.BrokerNo1
        select new Promotion() {Code=p.Code,BrokerName=b.Name}).ToList<Promotion>();

You can try the following to separate out the queries.

var promotions = from p in dc.Promotions
                 where p.Source == source
                 select p;

var brokers = from o in promotions
              join b in RDdc.BrokerNos on o.Broker equals b.BrokerNo1
              select new Promotion 
              {
                  Code = o.Code,
                  BrokerName = b.Name
              };
return brokers.ToList();

Upvotes: 1

Danny Varod
Danny Varod

Reputation: 18068

The double from looks suspicious to me. (This is not the equivalent to SQL's JOIN statement, if that is what you were aiming for.)

If you can combine the contexts:

Try creating relationship between BrokerNos and Promotions in edmx and using navigation property in query.

For example:

var result = dc.Promotions.Where(p => p.Source == source).
    Select(p => new Promotion() {
        Code = p.Code,
        BrokerName = p.Broker.Name, // use new navigation property here
    });

If not (intersection will be done in memory, not on DB!!!:

var result1 = dc.Promotions.Where(p => p.Source == source).
    Select(p => new Promotion() {
        Code = p.Code,
        BrokerId = p.BrokerId, // add id property for intermediate results
    }).ToList();   

var result2 = RDdc.Brokers.ToList();

var finalResult = result1.Where(p => result2.Contains(b => b.BrokerId == p.BrokerId)).Select(p => new Promotion{
        Code = p.Code,
        BrokerName = result2.Single(b => b.BrokerId == p.BrokerId).Name,
    });

Upvotes: 0

dthorpe
dthorpe

Reputation: 36082

Your linq statement looks fine. To aid in debugging, I find it helpful to assign the linq query to a local variable, then return the local variable. You can then set a breakpoint on the return statement, and when the debugger stops at the breakpoint, you can inspect the query local variable to see what's in it, interactively. You can use the Locals window in VS, or the Immediate Window to surf around inside your app's variables and see what's going on.

In particular, double check that the inputs into your linq query are actually providing data. Verify that RDdc.Brokernos is non-empty, and dc.Promotions, etc. If these are empty, the result will be empty. Track your bug "upstream".

Minor point: You don't need to specify the type parameter on the .ToList() call in the select. The compiler will infer the type automagically.

Upvotes: 2

Related Questions