Accepted answer

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();


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.


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

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,

Related Articles