LINQ选择并插入:我在这里太聪明了吗? (更新) [英] LINQ Select and insert: Am I trying to be too clever here? (updated)

查看:61
本文介绍了LINQ选择并插入:我在这里太聪明了吗? (更新)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我正在尝试使用Linq-to-SQL从数据库中选择一些值,并基于这些值插入一条新记录.在SQL中,我会这样写:

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:

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



我尝试使用linq扩展来编写此代码,如下所示:



I tried writing this using linq extensions like so:

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




每个[object] .Query(db)只是检查上下文(db)是否为空,仅当它为null时才创建一个新的数据库实例,然后将db.[object] s作为IQueriable< [object]>返回.任何[object] .Query ...()都可以执行某些操作来缩小IQueryable< [object]>的范围.设置,但每个检查db是否为null,并始终将其自己的实例最终传递到[object] .Query(db).我将下面的DecoPlatform类作为示例.

[DecoPlatform类]




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

    }




但是我收到错误``不允许在查询中显式构造实体类型''SQLDataAccess.CarmaEntityFramework.RunPlatform''.''.

问题:如何设法获得IEnumerable< runplatform>列表从linq?

目前,我分为两个阶段.首先是IEnumerable产品平台.选择,然后上下文将所有内容插入新的RunPlatform类''.

我可以使用此解决方案,但我希望问题很简单/容易调查.我的意思是,我有时间要学习,对吗? ^ _ ^

在此先感谢
G




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

推荐答案

您不能将实体创建为查询部分.
可以在查询之外创建实体,然后使用DataContext将其插入到数据存储中.
You can''t create entities as query part.
Entities can be created outside of query and inserted into the data-store using a DataContext.


这篇关于LINQ选择并插入:我在这里太聪明了吗? (更新)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆