简体   繁体   中英

Return LINQ Query and INSERT Data

I need to grab data from a table (Job) and copy that data into the corresponding columns on another table (JobDetails). I'm running a LINQ query that gets the data from the (Job) table that I will need to insert into the JobDetails table here:

 //Run query to get a list of all tasks that have the group ID passed
        IQueryable<Task> TasksQuery =
            from c in db.Tasks
            select c;
    //Execute and loop results...
    IQueryable<Task> TasksByGroupId = TasksQuery.Where(c => c.TaskGroupId == TasksGroupId);

    //TODO: Insert the results or list from above as a new JobDetails row

Now I'm at a complete and total loss in terms of extracting this data and inserting it back into specific columns on my JobDetails Table. I've tried doing a .ToList() in an attemp to create a readable list but I'm sure there is a more effective way of doing this. I would like to be able to one column at a time assign a variable and insert it into the corresponding row of my JobDetails table.

EDIT: Added Classes

public partial class Job
{
    public int Id { get; set; }
    public System.DateTime CreatedDate { get; set; }
    public string CreatedBy { get; set; }
    public Nullable<System.DateTime> ClosedDate { get; set; }
    public string ClosedBy { get; set; }
    public string Revision { get; set; }
}

public partial class JobDetail
{
    public int Id { get; set; }
    public int JobId { get; set; }
    public Nullable<int> TaskId { get; set; }
    public Nullable<int> TaskIdOrder { get; set; }
    public System.DateTime TimeCompleted { get; set; }
    public string CompletedBy { get; set; }
    public string Revision { get; set; }
}

EDIT: Added Task Class

public partial class Task

{
    public int Id { get; set; }
    public string TaskName { get; set; }
    public string TaskDescription { get; set; }
    public int TaskGroupId { get; set; }
    public int TaskOrder { get; set; }
}
foreach (var task in TasksByGroupId)
{
    db.JobDetails.AddObject(new JobDetail()
        {
            TaskId = task.Id,
            TaskIdOrder = task.TaskOrder
        });
}

db.SaveChanges();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM