将sql语法转换为entityspaces语法

时间:2013-09-05 21:50:12

标签: sql entityspaces

我是实体空间中的新手,并尝试将我的sql查询转换为实体空间格式

我有3张桌子

Course
Lesson
Attemps 

我还有一个视图'v_Course',我在查询中使用此视图

以下是我的SQL查询:

Select c.*,
   sub.res
FROM  (
  SELECT l.[CourseID],
     Count(l.CourseID) as res
  FROM   [Lesson] l
      INNER JOIN [Attempt] a
      ON (l.[LessonID] = a.[LessonID]
      AND l.[CourseID] IN (SELECT DISTINCT c.[CourseID]
                           FROM   [Course] c
                          )) group by l.CourseID
 ) AS sub  INNER JOIN v_Course c ON c.CourseID=sub.CourseID order by res desc

此查询在SQL查询浏览器中成功返回所需的结果

现在我想在实体空间中转换此语法,下面是代码

//Query for the join

AttemptQuery aq = new AttemptQuery("a");

//SubQuery of course id
CourseQuery cq = new CourseQuery("c");
cq.es.Distinct = true;
cq.Select(cq.CourseID);

LessonQuery lq = new LessonQuery("l");
lq.Select(lq.CourseID, lq.CourseID.Count().As("res"));
lq.InnerJoin(aq).On(lq.LessonID == aq.LessonID & lq.CourseID.In(cq));

lq.GroupBy(lq.CourseID);
lq.Load();

VCourseCollection vCourseColl = new VCourseCollection();
vCourseColl.Query.SelectAllExcept(vCourseColl.Query.CourseWriterName);
    vCourseColl.Query.Where(vCourseColl.Query.CourseIsDeleted.Equal(false),vCourseColl.Query.CourseIsActive.Equal(true), vCourseColl.Query.ActiveLessonCount.GreaterThan(0), vCourseColl.Query.CourseCategoryID.NotEqual(7));
vCourseColl.Query.OrderBy(vCourseColl.Query.CourseName.Ascending);
vCourseColl.Query.From(lq).As("sub");
vCourseColl.Query.InnerJoin(lq).On(vCourseColl.Query.CourseID==lq.CourseID);
vCourseColl.Query.Load();  

但是当我运行此页面时,我遇到了这种错误

SqlException was unhandled by the user code

Ambiguous column name 'CourseID'.

Invalid column name 'CourseCategoryID'.

Ambiguous column name 'CourseID'.

Invalid column name 'CourseName'.

Invalid column name 'CourseDescription'.

Invalid column name 'CourseIsDeleted'.

Invalid column name 'CourseImageID'.

Invalid column name 'CourseKeyWords'.

Invalid column name 'CourseIsActive'.

Invalid column name 'OutstandingIssues'.

Invalid column name 'CourseWriterEmployeeID'.

Invalid column name 'CourseAnimatorEmployeeID'.

Invalid column name 'CourseAnimatorName'.

Invalid column name 'TrainingTime'.

Invalid column name 'CourseCategoryName'.

Invalid column name 'SafetyImageID'.

Invalid column name 'FeedbackRatingAverage'.

Invalid column name 'CourseCategorySortOrder'.

Invalid column name 'TotalLessonCount'.

Invalid column name 'TotalLessonsInProduction'.

Invalid column name 'ActiveLessonCount'.

Invalid column name 'TrainingQuestionsTime'.

Invalid column name 'TrainingLessonsTime'.

我确信我正在实体空间中编写代码。

有人在这里帮助我...................

由于

(注意:我之前已经问了同样的问题,但格式化的代码很差,所以再次使用干净的代码再次询问)

1 个答案:

答案 0 :(得分:0)

请参阅http://brewdawg.github.io/Tiraggo.EF/(语法相同)

http://www.entityspaces.net/www.entityspaces.net/blog/2011/11/02/EntitySpaces%20Dynamic%20Query%20Showcase%20Repost.aspx.html

你犯的一个错误是调用lq.Load(),你永远不会直接通过查询加载,也不要在你的集合中使用内置查询,使用查询对象,这在最后调用Load(你的集合上的yourQuery),如果你没有回复它,我会帮助你编写查询,但看看我发布的那些样本。