Click here to Skip to main content
15,885,985 members
Please Sign up or sign in to vote.
5.00/5 (2 votes)
See more:
Hi
I am trying to use Linq-to-SQL to select some values from a db and insert a new record based on those values. In SQL I would write it like:

SQL
INSERT INTO RunPlatform(
platform_id,
build_env,
go_args,
--etc...
)SELECT
pp.platform_id,
pp.build_environment,
pp.go_args,
--etc...
FROM
DecoPlatforms dp
INNER JOIN ProductPlatforms pp ON dp.prodplat_id = pp.prodplat_id
WHERE dp.deco_id = 555


I tried writing this using linq extensions like so:
C#
db.RunPlatforms.InsertAllOnSubmit
    (
        DecoPlatform.QueryByDecoId(Run.GetDecoIdFromRun(runId, db),db)
        .Join(
                ProductPlatform.Query(db),
                dp => dp.prodplat_id,
                pp => pp.prodplat_id,
                (dp, pp) => pp
            )
            .Where(pp => buildOnlyNotTestOnly == null || pp.build_environment == ((bool)buildOnlyNotTestOnly ? "T" : "F"))
            .Select(pp => new RunPlatform
            {
                platform_id = pp.platform_id,
                build_env = pp.build_environment,
                go_arg = pp.go_arg,
                bits = pp.bits,
                baseline_id = (int)pp.baseline_id,
                t3_osmap = pp.t3_osmap
            })
    );



Each [object].Query(db) simply checks if context (db) is null, creates a new db instance only if it is null, and returns the db.[object]s as an IQueriable<[object]>. Any [object].Query...() may perform some actions to narrow the IQueryable<[object]> set but each checks the db for null and always passes it's own instance down the line finally to [object].Query(db). I have included the DecoPlatform class below as an example of this.

[DecoPlatform class]
C#
partial class DecoPlatform
    {
        public static IQueryable Query
            (
                CarmaContext db = null
            )
        {
            if (db == null)
                db = new CarmaContext();
            return db.DecoPlatforms;
        }

        public static IQueryable QueryByDecoId
            (
                int decoId,
                CarmaContext db = null
            )
        {
            if (db == null)
                db = new CarmaContext();
            return Query(db).Where(dp => dp.deco_id == decoId);
        }

    }



But I get the error 'Explicit construction of entity type 'SQLDataAccess.CarmaEntityFramework.RunPlatform' in query is not allowed.'.

Question: How do I manage to get the IEnumerable<runplatform> list from the linq?

For now I have two phases; First a IEnumerable<productplatform> select and then the context insert all with the new RunPlatform class'.

I could use this solution but I hoped the problem was simple / easy enough to investigate. I mean, I gotta learn sometime, right? ^_^

Thanks in advance
G
Posted
Updated 19-Sep-12 5:22am
v5
Comments
Andrei Straut 19-Sep-12 10:43am    
I have moved the code you posted as comment to your question body. Feel free to delete your comment if you no longer need it, and upvoted your question too. Also, you can edit your own question as well, if you feel you need to bring further clarifications or edits
Greysontyrus 19-Sep-12 10:46am    
Thx Andrei ^_^
Andrei Straut 19-Sep-12 10:47am    
You're welcome. Sorry I can't actually help you with your question as my LINQ experience is somewhat limited, but maybe someone else can chime in and help
Greysontyrus 19-Sep-12 11:22am    
The error message and question have changed

1 solution

You can't create entities as query part.
Entities can be created outside of query and inserted into the data-store using a DataContext.
 
Share this answer
 
Comments
Greysontyrus 10-Oct-12 5:53am    
The problem there is that if the query is regarding a large volume of data then that data has to be transported raw from the database to the web server and back again. MicroSoft have decided that developers don't know what they are doing so they have restricted us from writing elegant insert queries in Linq in 4.0. Thanks MicroSoft for changing my nappy >_<

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900