Click here to Skip to main content
11,803,019 members (46,886 online)
Rate this: bad
Please Sign up or sign in to vote.
See more: C# .NET LINQ C#4.0

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:

INSERT INTO RunPlatform(
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:
        DecoPlatform.QueryByDecoId(Run.GetDecoIdFromRun(runId, db),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]
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 list from the linq?

For now I have two phases; First a IEnumerable 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
Posted 19-Sep-12 4:28am
Edited 19-Sep-12 5:22am
Andrei Straut at 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 at 19-Sep-12 10:46am
Thx Andrei ^_^
Andrei Straut at 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 at 19-Sep-12 11:22am
The error message and question have changed

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 2

You can't create entities as query part.
Entities can be created outside of query and inserted into the data-store using a DataContext.
Greysontyrus at 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)

  Print Answers RSS
0 OriginalGriff 335
1 KrunalRohit 291
2 F-ES Sitecore 270
3 CPallini 270
4 Sergey Alexandrovich Kryukov 234
0 OriginalGriff 2,950
1 Maciej Los 1,910
2 KrunalRohit 1,862
3 CPallini 1,735
4 Richard MacCutchan 1,157

Advertise | Privacy | Mobile
Web03 | 2.8.151002.1 | Last Updated 20 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100