linq查询从两个数据库 [英] linq query from two database

查看:112
本文介绍了linq查询从两个数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个数据库的linq查询,但每次程序停止在查询点。我不知道如何使用VS来调试linq。有人可以帮我弄清楚这里有什么问题吗?谢谢。

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


推荐答案

您可以使用以下显示生成的Linq语句的SQL。

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

这篇关于linq查询从两个数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆