【问题标题】:SQL into LINQ to EntitiesSQL 到 LINQ to Entity
【发布时间】:2011-06-03 10:21:41
【问题描述】:

我有一个大问题。

我是过去 5 年的 SQL 男孩,但现在我需要将我的 SQL 查询转换为 LINQ 到实体 C# 格式。 因为我现在是 LINQ(复杂语句)的新手,所以我需要快速帮助。

提前致谢。

附:我还需要一些建议,一些快速开始学习 LINQ to entity 的起点。

这是我的 SQL(直接来自我的应用程序(@endDate、@endDate 和 @glChartID 在我的 c# 应用程序中也作为参数保留)):

SELECT budget.accountid,   
budget.perioddate,
budget.lastyear,
budget.thisyear,   
budget.budget,   
budget.operplan,
budget.forecast,   
glchart.accounttype,
glchart.headertype

FROM budget INNER JOIN glchart ON budget.accountid = glchart.id
WHERE budget.accountid = @glChartID AND budget.perioddate BETWEEN @startDate and @endDate AND glchart.headertype NOT LIKE 'Header%'

UNION

SELECT  glchart.id,   
budget.perioddate,   
SUM(ISNULL(budget.lastyear, 0)),   
SUM(ISNULL(budget.thisyear, 0)),    
SUM(ISNULL(budget.budget, 0)),    
SUM(ISNULL(budget.operplan, 0)),  
SUM(ISNULL(budget.forecast, 0)),  
glchart.accounttype,
glchart.headertype

FROM budget INNER JOIN glchart ON budget.accountid = glchart.id
WHERE budget.accountid  
IN 
(SELECT g.id FROM glchart g
WHERE g.code >= glchart.code AND g.code <  

CASE
WHEN glchart. headerlevel = 1 AND
(SELECT MAX(g3.code)
FROM glchart g3
WHERE g3.headerlevel = 1
) = glchart.code
THEN 
(SELECT MAX(g2.code)
FROM glchart g2
WHERE g2.code >= g.code)
ELSE
(SELECT MIN(g2.code)
FROM glchart g2
WHERE g2.code > glchart.code AND
g2.headerlevel  = glchart. headerlevel) END ) AND
glchart.id = @glChartID AND
budget.perioddate BETWEEN @startDate AND @endDate AND
glchart.headertype LIKE 'Header%'

GROUP BY glchart.id, budget.perioddate, glchart.accounttype, glchart.headertype

直到今天,我才(感谢 DOK)设法做到这一点,这就是我的 LINQ 现在的样子:

var query = ((ObjectQuery<Budget>)(                

                            from budgets in this.ObjectContext.Budgets

                            join glcharts in this.ObjectContext.GLCharts on new { AccountID = budgets.AccountID } equals new { AccountID = glcharts.ID }
                            where
                                    (!(from glC in this.ObjectContext.GLCharts
                                     where Convert.ToInt16(glC.Code) >= Convert.ToInt16(glcharts.Code) && glC.Code != (Convert.ToInt64(glcharts.HeaderLevel) == 1 &&

                                         (from g3 in this.ObjectContext.GLCharts
                                             where  Convert.ToInt64(g3.HeaderLevel) == 1
                                              select new {g3.Code}).Max(p => p.Code) == glcharts.Code ? 
                                                (from g2 in this.ObjectContext.GLCharts
                                                    where Convert.ToInt16(g2.Code) >= Convert.ToInt16(glC.Code)
                                                      select new {g2.Code}).Max(p => p.Code) : 
                                                (from g2 in this.ObjectContext.GLCharts
                                                     where Convert.ToInt16(g2.Code) > Convert.ToInt16(glcharts.Code) && g2.HeaderLevel == glcharts.HeaderLevel
                                                      select new {g2.Code}).Min(p => p.Code))
                                    select new {glC.ID}

                                 ).Contains(new { budgets.AccountID }) &&  

                                 glcharts.ID == 2376 && budgets.PeriodDate >= StartDate && 
                                 budgets.PeriodDate <= EndDate && 
                                 glcharts.HeaderType.StartsWith("Header"))

                                 ).Contains(new { budgets.AccountID }) &&  glcharts.ID == 2376 && budgets.PeriodDate >= StartDate && budgets.PeriodDate <= EndDate && glcharts.HeaderType.StartsWith("Header")

                                   group new {glc = glcharts, b = budgets} 
                                    by new {
                                     glcharts.ID,
                                     budgets.PeriodDate,
                                     glcharts.AccountType,
                                     glcharts.HeaderType
                                    } into g

                            select new {
                              AccountID = (System.Int32?)g.Key.ID,
                              PeriodDate = (System.DateTime?)g.Key.PeriodDate,
                              LastYear = g.Sum(p => ((System.Decimal?)p.t.LastYear ?? (System.Decimal?)0)),
                              ThisYear = g.Sum(p => ((System.Decimal?)p.t.ThisYear ?? (System.Decimal?)0)),
                              Budget = g.Sum(p => ((int?)p.t.Budget1 ?? (int?)0)), 
                              OperPlan = g.Sum(p => ((System.Decimal?)p.t.OperPlan ?? (System.Decimal?)0)),
                              Forecast = g.Sum(p => ((System.Decimal?)p.t.Forecast ?? (System.Decimal?)0)),
                              AccountType = g.Key.AccountType,
                              HeaderType = g.Key.HeaderType
                                        }));

        return query;

但在这条线:.Contains(new { budgets.AccountID }) 我收到下一个错误:

错误 8'System.Linq.IQueryable' 不包含 'Contains' 的定义,并且最佳扩展方法重载 'System.Linq.ParallelEnumerable.Contains(System.Linq.ParallelQuery, TSource)' 有一些无效参数

有人知道我哪里错了吗?

谢谢大家。

【问题讨论】:

    标签: c# sql linq linq-to-entities


    【解决方案1】:

    您可能会在此excellent reference site 中找到一些帮助。

    这将引导您访问,例如,two examples for UNION

    如果您真的必须从这个难度级别开始,您可能会考虑将您的 SQL 分解为多个部分,并让它们一点一点地工作。在没有JOINWHERE 的情况下执行第一个SELECT,然后一次添加它们。然后以同样的方式执行第二个SELECT。然后添加UNION

    当你解决这个问题时,SQL 小子,你肯定会成为 LINQ 人!

    【讨论】:

    • 您好,DOK,感谢您的支持。你给我的链接非常有用,所以我设法将我的 SQL 重写为 LINQ to Entities。
    • 我很高兴听到这个消息,德扬。并感谢您编辑您的答案以显示您的结果。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-04-27
    • 2011-11-29
    • 2021-07-12
    • 2010-10-30
    • 1970-01-01
    相关资源
    最近更新 更多