SQL到LINQ to Entities

时间:2011-06-03 10:21:42

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

我有一个大问题。

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

提前感谢。

P.S。我也需要一些建议,一些起点可以迅速开始学习实体的LINQ。

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

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)'有一些无效的参数< / p>

有人知道我哪里错了吗?

感谢大家。

1 个答案:

答案 0 :(得分:6)

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

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

如果你真的必须从这个难度级别开始,你可能会考虑将你的SQL分解成碎片并让它们一点一点地工作。在没有SELECTJOIN的情况下执行第一个WHERE,然后一次添加一个SELECT。然后以相同的方式执行第二个UNION。然后添加{{1}}。

当你得到这个时,SQL-boy,你肯定会成为LINQ-man!