【问题标题】:What is the linq query of below sql statements?以下 sql 语句的 linq 查询是什么?
【发布时间】:2015-10-27 09:25:04
【问题描述】:

谁能解释一下如何为这个 SQL 查询编写等效的 LINQ 查询

select  t1.Description,t2.Description,t1.ReponseTypeID 
from tblResponseType t1
   inner join tblPartnerServiceResponseType t2 
      on t1.ReponseTypeID = t2.ReponseTypeID
where t1.ReponseTypeID = (select ReponseTypeID
                          from tblPartnerServiceResponseType
                          where PartnerServiceID = 1);

我是新手

【问题讨论】:

    标签: .net sql-server linq


    【解决方案1】:
    db.tblResponseTypeEntitieDB.Where(el => el.ResponseType.Id = 1).ToList()
    

    条件:

    • 关系在您的实体中正确填写,...

    另外,我们没有太多的上下文...

    【讨论】:

      【解决方案2】:

      我们可以先尝试两个查询:

      var psrt = tblPartnerServiceResponseType.FirstOrDefault(srt => srt.PartnerServiceID == 1);
      if (psrt != null)
      {
          var rt = tblResponseType.FirstOrDefault(rt => rt.ReponseTypeID == psrt.ReponseTypeID);
      }
      

      这可行,但需要两次往返数据服务器。

      好的,接下来,假设您已经正确设置了外键,因此 tblPartnerServiceResponseType 在其中有一个 tblResponseType 属性:

      var query = from t2 in tblPartnerServiceResponseType
                  where t2.PartnerServiceID == 1
                  select new {Desc1 = t2.tblResponseType.description, 
                              Desc2 = t2.Description,
                              t2.ReponseTypeID
                              };
      

      最后,如果您没有设置外键(您真的应该这样做!),那么我们必须进行显式连接:

      var query = from t2 in tblPartnerServiceResponseType
                  join t1 in tblResponseType on t2.ReponseTypeID equals  t1.ReponseTypeID
              where t2.PartnerServiceID == 1
              select new {Desc1 = t1.Description, 
                          Desc2 = t2.Description,
                          t2.ReponseTypeID
                          };
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2017-09-25
        • 1970-01-01
        • 2011-12-22
        • 1970-01-01
        • 2015-03-03
        • 1970-01-01
        • 2014-01-23
        相关资源
        最近更新 更多