简体   繁体   中英

Convert sql query to entity framework sql

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.

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;}
  ....
}

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM