实体查询选择top1

时间:2012-03-27 04:37:55

标签: c# asp.net sql-server-2008 entity-framework

我使用 ExecuteStoreQuery 在sql中遇到问题请参阅下面的示例

我的编码

  private void ChildMenuItem()
    {
        using (LEWREDBEntities ctx = new LEWREDBEntities())
        {
            string result = @"

                WITH ctLevel
                AS
                (
                    SELECT
                        C_TASK_ID                                                               AS Child
                        ,P_Task_ID                                                              AS Parent
                        ,common_task.   TASK_SEQ                                                AS taskSeq
                        ,1                                                                      AS [Level]
                        ,CAST( TASK_SEQ AS VARCHAR(MAX))                                        AS [taskOrder]
                        ,CAST (Replicate('', 1) + common_task.TASK_NAME AS VARCHAR(MAX))        AS [Task_Name]
                    FROM   
                        [COMMON.TASK_REL] as common_task_rel, 
                        [COMMON.TASK] as common_task
                    WHERE  
                        common_task_rel.C_TASK_ID = common_task.TASK_ID
                        and     common_task.[TASK_TYPE] = 'F' AND common_task.[MODULE_CODE] = 'PRODE' AND common_task.[STATUS] <> 'D'
                        and C_TASK_ID =357

                    UNION ALL

                    SELECT 
                        C_TASK_ID                                                                   AS Child
                        ,P_Task_ID                                                                  AS Parent
                        ,common_task.   TASK_SEQ                                                    AS taskSeq
                        ,[Level] + 1                                                                AS [Level]
                        ,[taskOrder] + '.' + CAST(TASK_SEQ AS VARCHAR(MAX))                         AS [taskOrder]
                        ,CAST (Replicate('', [Level] + 1) + common_task.TASK_NAME AS VARCHAR(MAX))  AS [Task_Name]
                    FROM   
                        [COMMON.TASK_REL]as common_task_rel
                    INNER JOIN 
                        ctLevel
                    ON 
                        ( P_Task_ID = Child ) ,
                        [COMMON.TASK] as common_task

                    WHERE  
                            common_task_rel.C_TASK_ID = common_task.TASK_ID
                    and     common_task.[TASK_TYPE] = 'F' AND common_task.[MODULE_CODE] = 'PRODE' AND common_task.[STATUS] <> 'D'
                    )

                   SELECT 
                   common_task.Task_Name,
                   common_task_url.TASK_URL,
                   ctLevel.CHILD

                    FROM   
                    ctLevel,
                    [COMMON.ACL] as common_acl,
                    [COMMON.STAFF_ROLE] as common_staff_role,
                    [COMMON.TASK_URL] as common_task_url,
                    [COMMON.TASK] as common_task,
                    [COMMON.STAFF_MODULE] as common_staff_module,
                    [common.module] as common_module

                    where ctLevel.Level =3
                    and ctLevel.Child = common_acl.TASK_ID
                    and common_acl.READ_ACCESS ='Y'
                    and common_acl.STATUS <>'D'
                    and common_acl.ROLE_ID = common_staff_role.ROLE_ID
                    and common_staff_role.STATUS <>'D'
                    and common_staff_role.STAFF_ID ='user'
                    and common_staff_role.STAFF_ID = common_staff_module.STAFF_ID
                    and common_staff_module.MODULE_CODE =common_module.MODULE_CODE
                    and common_staff_module.STATUS<>'D'
                    and common_staff_module.MODULE_CODE = common_module.MODULE_CODE
                    and common_module.STATUS <>'D' and common_module.MODULE_CODE ='PRODE'
                    and common_acl.TASK_ID = common_task.TASK_ID
                    and common_task.STATUS <>'D'
                    and common_task.TASK_TYPE ='F'
                    and common_task.TASK_POSITION ='S'
                    and common_task.TASK_ID = common_task_url.TASK_ID
                    and common_task_url.DEFAULT_URL ='Y'
                    and common_task_url.STATUS<>'D'


                    Group By    
                             [taskOrder],common_task.Task_Name, TASK_URL,ctLevel.CHILD

                    order by [taskOrder],common_task.Task_Name, TASK_URL,ctLevel.CHILD";

            var query = ctx.ExecuteStoreQuery<dlcGvTask>
                (result);


            foreach (dlcGvTask gvTask in query.ToList())
            {
                MenuItem navigation = new MenuItem(gvTask.TASK_NAME, gvTask.Child.ToString(), "","");
                MenuChild.Items.Add(navigation);
            }

          //Here????
            var id = (from table in query
                      select table.Child).Take(1);

            ;

        }

    }

现在我有了一个问题。如何选择记录的前1位?

2 个答案:

答案 0 :(得分:1)

在您的代码中

  //Here????
    var id = (from table in query
              select table.Child).Take(1);

尝试

var id = query.First();
//Or 
var id = query.Take(1);

答案 1 :(得分:0)

尝试做这样的事情: -

Select Top 1 ..... ;

OR

 ..... Where ROW_NUMBER() = 1;