0

我是实体空间的新手,并试图将我的 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'.

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

请任何人在这里帮助我......

谢谢

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

4

1 回答 1

1

请参阅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) on你的收藏,如果你没有把它发回来,我会帮你写查询,但是看看我发布的那些样本。

于 2013-11-04T12:16:54.980 回答