SQL в LINQ to Entities

У меня большая проблема.

Я последний 5-летний SQL-мальчик, но теперь мне нужно преобразовать свой SQL-запрос в формат LINQ to entity C #. Поскольку я новичок в LINQ (сложные операторы) прямо сейчас, мне нужна быстрая помощь.

Заранее благодарю.

P.S. также мне нужны несколько советов, некоторая начальная точка для быстрого начала изучения 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) 'содержит недопустимые аргументы

Кто-нибудь знает, в чем я ошибаюсь?

Спасибо всем.

0
задан Dejan Petrovic 6 June 2011 в 07:43
поделиться