linq query from two database

时光毁灭记忆、已成空白 提交于 2019-12-12 18:40:37

问题


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

回答1:


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



回答2:


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.




回答3:


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,
    });


来源:https://stackoverflow.com/questions/6048718/linq-query-from-two-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!