How to do INSERT INTO X(Y,Z) SELECT(Y,Z FROM X WHERE ...)
Posted: Thu 10 Mar 2011 11:15
Hi,
I want to perform the followinq sql query in LinqToSql
INSERT INTO X(Y,Z) SELECT(Y,Z FROM X WHERE ...)
I tried the following :
var queryToInsert = from c in ObjectContext.EntiteGroupes
where c.IdGroupe == oldId
select new EntiteGroupe
{
IdGroupe = groupe.IdGroupe,
IdEntite = c.IdEntite,
TypeDroit = c.TypeDroit
};
ObjectContext.EntiteGroupes.InsertAllOnSubmit(queryToInsert);
Or the same version with another syntax :
ObjectContext.EntiteGroupes.InsertAllOnSubmit(
ObjectContext
.EntiteGroupes
.Where(c => c.IdGroupe == oldId)
.Select(b => new EntiteGroupe { IdGroupe = groupe.IdGroupe, IdEntite = b.IdEntite, TypeDroit = b.TypeDroit })
);
but I got an exception : Explicit construction of entity type 'EntiteGroupe' in query is not allowed.
The aim of the query is to copy rows of EntiteGroupe of a group to a new one without having x queries insert to perform.
This is really important because it's a huge performance limitation.
I know that the alternative is to use ObjectContext.ExecuteCommand, like "ObjectContext.ExecuteCommand("INSERT INTO ENTITE_GROUPE(ID_ENTITE, ID_GROUPE, TYPE_DROIT, RM, EM)(SELECT ID_ENTITE, {0}, TYPE_DROIT, {1}, {2} FROM ENTITE_GROUPE WHERE ID_GROUPE = {3})", groupe.IdGroupe, groupe.Rm, groupe.Em, ancienId);
"
but we lose the benefit of Linq controlling type, name and so on.
I read somewhere that this syntax was okay at the beginning of Linq but was denied cause it could "pollutes the cache with potentially malformed objects" but I'm not interested in using the object created by the request.
With devart, is there the same limitation (it seems), is there an alternative having linq generates the sql query expected ?
Best regards.
I want to perform the followinq sql query in LinqToSql
INSERT INTO X(Y,Z) SELECT(Y,Z FROM X WHERE ...)
I tried the following :
var queryToInsert = from c in ObjectContext.EntiteGroupes
where c.IdGroupe == oldId
select new EntiteGroupe
{
IdGroupe = groupe.IdGroupe,
IdEntite = c.IdEntite,
TypeDroit = c.TypeDroit
};
ObjectContext.EntiteGroupes.InsertAllOnSubmit(queryToInsert);
Or the same version with another syntax :
ObjectContext.EntiteGroupes.InsertAllOnSubmit(
ObjectContext
.EntiteGroupes
.Where(c => c.IdGroupe == oldId)
.Select(b => new EntiteGroupe { IdGroupe = groupe.IdGroupe, IdEntite = b.IdEntite, TypeDroit = b.TypeDroit })
);
but I got an exception : Explicit construction of entity type 'EntiteGroupe' in query is not allowed.
The aim of the query is to copy rows of EntiteGroupe of a group to a new one without having x queries insert to perform.
This is really important because it's a huge performance limitation.
I know that the alternative is to use ObjectContext.ExecuteCommand, like "ObjectContext.ExecuteCommand("INSERT INTO ENTITE_GROUPE(ID_ENTITE, ID_GROUPE, TYPE_DROIT, RM, EM)(SELECT ID_ENTITE, {0}, TYPE_DROIT, {1}, {2} FROM ENTITE_GROUPE WHERE ID_GROUPE = {3})", groupe.IdGroupe, groupe.Rm, groupe.Em, ancienId);
"
but we lose the benefit of Linq controlling type, name and so on.
I read somewhere that this syntax was okay at the beginning of Linq but was denied cause it could "pollutes the cache with potentially malformed objects" but I'm not interested in using the object created by the request.
With devart, is there the same limitation (it seems), is there an alternative having linq generates the sql query expected ?
Best regards.