1

I need to manage hierarchy data storing in my database. But now I am facing a problem. I am using entity sql for my asp.net. So now, how can I convert those sql to entity query? Here is the SQL query I suppose converts to entity framework query

      WITH RPL ( P_TASK_ID, C_TASK_ID,taskSeq) AS
     (  SELECT  root.P_TASK_ID, root.C_TASK_ID ,root.Seq
        FROM [COMMON.TASK_REL_test] as root
      UNION ALL
        SELECT  child.P_TASK_ID, child.C_TASK_ID, child.Seq
        FROM [COMMON.TASK_REL_test] parent, [COMMON.TASK_REL_test] child
        WHERE  parent.C_TASK_ID = CHILD.P_TASK_ID
     )
SELECT DISTINCT P_TASK_ID, C_TASK_ID,taskSeq
 FROM RPL
  ORDER BY  P_TASK_ID, C_TASK_ID,taskSeq; 

Here is my table structure

   pID  CID   Seq
   NULL 1   1
    1   2   1
    1   3   2
    1   4   3
    2   5   1
    2   6   2
    3   7   1

Here is my insert table query

INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID, Seq)
values (null,1,1)


INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID,Seq)
values (1,2,1)


INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID,Seq)
values (1,4,3)


INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID,Seq)
values (1,3,2)


INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID,Seq)
values (2,6,2)

INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID,Seq)
values (2,5,1)

INSERT into [COMMON.TASK_REL_test](P_TASK_ID,C_TASK_ID,Seq)
values (3,7,1)

Here is my coding

 private void createGridView()
        {
            try
            {


                using (ObjectContext ctx = new ObjectContext(gbcDbConnection.eObjqueryConnection))
                {
                    string result = @"
                                ;WITH RPL ( P_TASK_ID, C_TASK_ID,taskSeq) AS
                                (  SELECT  root.P_TASK_ID, root.C_TASK_ID ,root.Seq
                                   FROM LEWREDBEntities.[COMMON_TASK_REL_test] as root
                         UNION ALL
                                SELECT  child.P_TASK_ID, child.C_TASK_ID, child.Seq
                                 FROM LEWREDBEntities.[COMMON_TASK_REL_test] as parent, LEWREDBEntities.[COMMON_TASK_REL_test] as child
                        WHERE  parent.C_TASK_ID = CHILD.P_TASK_ID
                    )
                         SELECT DISTINCT P_TASK_ID, C_TASK_ID,taskSeq
                         FROM RPL
                         ORDER BY  P_TASK_ID, C_TASK_ID,taskSeq";
                    ObjectQuery<DbDataRecord> query = ctx.CreateQuery<DbDataRecord>(result);
                    string cde = query.ToTraceString();
                }
            }
            catch (Exception ex)
            {
                throw new ApplicationException(ex.Message);
            }

        }

But I am getting an error now.
The query syntax is not valid. Near term ';', line 2, column 34..
Please help.

3 Answers 3

4

Missing ;

string result = " ; WITH RPL

edit

Well, ok, I googled a bit and the problem is CreateQuery uses EntitySql syntax, not T-SQL, so here the problem.

You can use ExecuteStoreQuery method:

string result = @" ... "

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

Where result is a class mapped to the projected columns:

public class Result
{
  public int P_TASK_ID {get; set;}
  ....
}
Sign up to request clarification or add additional context in comments.

1 Comment

Hi i try adding " ; WITH RPL ( P_TASK_ID, C_TASK_ID,taskSeq) AS ";. Now i get "The query syntax is not valid. Near term ';', line 1, column 3."
0

You are missing ; at the start. And why are you += the string. You can just do like this:

string result=@"
                ;WITH RPL ( P_TASK_ID, C_TASK_ID,taskSeq) AS
                    (  SELECT  root.P_TASK_ID, root.C_TASK_ID ,root.Seq
                        FROM [COMMON.TASK_REL_test] as root
                    UNION ALL
                        SELECT  child.P_TASK_ID, child.C_TASK_ID, child.Seq
                        FROM [COMMON.TASK_REL_test] parent, [COMMON.TASK_REL_test] child
                        WHERE  parent.C_TASK_ID = CHILD.P_TASK_ID
                    )
                SELECT DISTINCT P_TASK_ID, C_TASK_ID,taskSeq
                FROM RPL
                ORDER BY  P_TASK_ID, C_TASK_ID,taskSeq";

Use the @" sign. Easier to read.

1 Comment

Hi , i update my code. Please help me solve the error.THans :)
0

If you'll have problems in future in can use Linker. This app can convert sql to linq code due to model. In fact while analysing very difficalt query i realised that it's impossible to full control output t-sql but anyway it was extremly helpfull

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.