时间:2019-03-17 标签:c#Lambda子查询(linq2sql)

发布于 2024-10-17 02:55:56 字数 1135 浏览 0 评论 0原文

是否可以在 Lambda 中编写与此 TSQL 语句等效的语句?

SELECT  DISTINCT
        pba1.adGroupsId
,      Amount = (SELECT pba2.value 
                  FROM   ProjectBudgetAdjustment pba2 
                  WHERE  pba1.budgetId = pba2.budgetId 
                  AND    pba1.adGroupsId = pba2.adGroupsId 
                  AND    pba2.metricId IN 
                  (
                      SELECT  id 
                      FROM    Metrics 
                      WHERE   [description] = 'Amount')
                  )
,      [Hours] = (SELECT pba2.value 
                   FROM   ProjectBudgetAdjustment pba2 
                   WHERE  pba1.budgetId = pba2.budgetId 
                   AND    pba1.adGroupsId = pba2.adGroupsId 
                   AND    pba2.metricId IN 
                  (
                      SELECT id 
                      FROM   Metrics 
                      WHERE  [description] = 'Hours')
                  )
FROM    ProjectBudgetAdjustment pba1
WHERE   pba1.budgetId IN
(
   SELECT  id
   FROM ProjectBudget
   WHERE   projectId IN
   (
      SELECT  id
      FROM    Projects
      WHERE   code = 'xxx'
   )
)

Is it possible to write the equivalent of this TSQL statement in Lambda?

SELECT  DISTINCT
        pba1.adGroupsId
,      Amount = (SELECT pba2.value 
                  FROM   ProjectBudgetAdjustment pba2 
                  WHERE  pba1.budgetId = pba2.budgetId 
                  AND    pba1.adGroupsId = pba2.adGroupsId 
                  AND    pba2.metricId IN 
                  (
                      SELECT  id 
                      FROM    Metrics 
                      WHERE   [description] = 'Amount')
                  )
,      [Hours] = (SELECT pba2.value 
                   FROM   ProjectBudgetAdjustment pba2 
                   WHERE  pba1.budgetId = pba2.budgetId 
                   AND    pba1.adGroupsId = pba2.adGroupsId 
                   AND    pba2.metricId IN 
                  (
                      SELECT id 
                      FROM   Metrics 
                      WHERE  [description] = 'Hours')
                  )
FROM    ProjectBudgetAdjustment pba1
WHERE   pba1.budgetId IN
(
   SELECT  id
   FROM ProjectBudget
   WHERE   projectId IN
   (
      SELECT  id
      FROM    Projects
      WHERE   code = 'xxx'
   )
)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

狼亦尘 2024-10-24 02:55:56

我在这里可能完全错了,但它是这样的吗:

ProjectBudgetAdjustment
.Where( pba => pba.ProductBudget.Products.Code == "xxx")
.Where( pba => (pba.Metrics.Description == "Hours" 
             || pba.Metrics.Description == "Amount"))
.GroupBy(   pba => pba.ProductBudget.adGroupsId )
.Select (
    r => new
    {
        adGroupsId = r.Key,
        Hours  = r.Sum(i=> (i.Metrics.Description == "Hours"  ? i.value : 0m)),
        Amount = r.Sum(i=> (i.Metrics.Description == "Amount" ? i.value : 0m))
    }
);

I could be total wrong here, but is it something like :

ProjectBudgetAdjustment
.Where( pba => pba.ProductBudget.Products.Code == "xxx")
.Where( pba => (pba.Metrics.Description == "Hours" 
             || pba.Metrics.Description == "Amount"))
.GroupBy(   pba => pba.ProductBudget.adGroupsId )
.Select (
    r => new
    {
        adGroupsId = r.Key,
        Hours  = r.Sum(i=> (i.Metrics.Description == "Hours"  ? i.value : 0m)),
        Amount = r.Sum(i=> (i.Metrics.Description == "Amount" ? i.value : 0m))
    }
);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文