SELECT INTO with EF4

I am new to C #, linq and EF4, so please bear with me. I am sure that it is really simple, but not seeing it.

The stored procedure that I am replacing does a SELECT INTO to query a set of data, performs some simple transformations, and then outputs the results to another table. This data set is then returned for local processing in code.

I have a query that outputs my data in an anonymous type and processes all the transformations. But how can I send this data to the result table?

At first, I thought about using an entity type to store my initial results. But I have additional fields that I need for local processing, which will not be saved in the result table.

Thanks to all the EF masters who want to hack this for me!

edit: Here some Morteza-based pseudo code helps, but doesn't seem to move data -

var ctx = new ReportEntities();
var query = from s in ctx.Source
            select new
            {
                s.SourceID,
                s.OtherStuff
            };

query.ToList().Select(q => new Report()
{
    SourceID = q.SourceID,
    OtherStuff = q.OtherStuff
});

ctx.SaveChanges();
+3
source share
1 answer

With EF4, you can update your model with separate stored procedures and import them as functions. I think the easiest way is to use this new function and create a second procedure that takes the result data as parameters and saves them in the result table. Thus, you can call this function after processing the data.

Update:

var ctx = new ReportEntities();
var query = (from s in ctx.Source select new {
        s.SourceID,
        s.OtherStuff}).ToList();

List<Report> reports = query.Select(q => new Report() 
{
    SourceID = q.SourceID,
    OtherStuff = q.OtherStuff
}).ToList();

// Now you need to add your new report objects to the Context:
foreach(report in reports) {
    ctx.Reports.AddObject(report);
}

// Now is the time to call the SaveChanges on ObjectContext:
ctx.SaveChanges();
+1
source

Source: https://habr.com/ru/post/1773249/


All Articles