Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8161255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T18:27:37+00:00 2026-06-06T18:27:37+00:00

In order to create a more elegant solution I’m curios to know your suggestion

  • 0

In order to create a more elegant solution I’m curios to know your suggestion about a solution to persist a collection.

I’ve a collection stored on DB.
This collection go to a webpage in a viewmodel.
When the go back from the webpage to the controller I need to persist the modified collection to the same DB.

The simple solution is to delete the stored collection and recreate all rows.
I need a more elegant solution to mix the collections and delete not present record, update similar records ad insert new rows.

this is my Models and ViewModels.

public class CustomerModel
{
    public virtual string Id { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<PreferredAirportModel> PreferedAirports { get; set; }
}

public class AirportModel
{
    public virtual string Id { get; set; }
    public virtual string AirportName { get; set; }
}

public class PreferredAirportModel
{
    public virtual AirportModel Airport { get; set; }
    public virtual int CheckInMinutes { get; set; }
}

// ViewModels
public class CustomerViewModel
{
    [Required]
    public virtual string Id { get; set; }
    public virtual string Name { get; set; }

    public virtual IList<PreferredAirporViewtModel> PreferedAirports { get; set; }
}

public class PreferredAirporViewtModel
{
    [Required]
    public virtual string AirportId { get; set; }

    [Required]
    public virtual int CheckInMinutes { get; set; }
}

And this is the controller with not elegant solution.

public class CustomerController
{
    public ActionResult Save(string id, CustomerViewModel viewModel)
    {
        var session = SessionFactory.CurrentSession;

        var customer = session.Query<CustomerModel>().SingleOrDefault(el => el.Id == id);

        customer.Name = viewModel.Name;

        // How can I Merge collections handling delete, update and inserts ?

        var modifiedPreferedAirports = new List<PreferredAirportModel>();
        var modifiedPreferedAirportsVm = new List<PreferredAirporViewtModel>();

        // Update every common Airport
        foreach (var airport in viewModel.PreferedAirports)
        {
            foreach (var custPa in customer.PreferedAirports)
            {
                if (custPa.Airport.Id == airport.AirportId)
                {
                    modifiedPreferedAirports.Add(custPa);
                    modifiedPreferedAirportsVm.Add(airport);

                    custPa.CheckInMinutes = airport.CheckInMinutes;
                }
            }
        }

        // Remove common airports from ViewModel
        modifiedPreferedAirportsVm.ForEach(el => viewModel.PreferedAirports.Remove(el));

        // Remove deleted airports from model
        var toDelete = customer.PreferedAirports.Except(modifiedPreferedAirports);
        toDelete.ForEach(el => customer.PreferedAirports.Remove(el));

        // Add new Airports
        var toAdd = viewModel.PreferedAirports.Select(el => new PreferredAirportModel
                                                            {
                                                                Airport =
                                                                            session.Query<AirportModel>().
                                                                            SingleOrDefault(a => a.Id == el.AirportId),
                                                                CheckInMinutes = el.CheckInMinutes
                                                             });
        toAdd.ForEach(el => customer.PreferedAirports.Add(el));

        session.Save(customer);

        return View();
    }
}

My environment is ASP.NET MVC 4, nHibernate, Automapper, SQL Server.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T18:27:38+00:00Added an answer on June 6, 2026 at 6:27 pm

    Well, if “elegant” is just “don’t clear and recreate all” (untested) :

    var airports = customer.PreferedAirports;
    var viewModelAirports = viewModel.PreferredAirports;
    
    foreach (var airport in airports) {
      //modify common airports
       var viewModelAirport = viewModelAirports.FirstOrDefault(m => m.AirportId == airport.AirportId);
       if (viewModelAirport != null) {
          airport.X = viewModelAirport.X;
          airport.Z = viewModelAirport.Z;
          //remove commonAirports from List
          viewModelAirports.Remove(viewModelAirport);
          continue;
       }
       //delete airports not present in ViewModel
       customer.PreferedAirports.Remove(airport);
    }
    
    //add new airports
    foreach (var viewModelAirport in viewModelAirports) {
       customer.PreferedAirports.Add(new PreferredAirportModel {
                 Airport = session.Query<AirportModel>().SingleOrDefault(a => a.Id == el.AirportId), 
                 CheckInMinutes = el.CheckInMinutes
                 });
    }
    session.Save(customer);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of XML that I use in order to create a
I'm reading Reference Guide::Create Your Project . And it's written there: In order to
In order to create interactive fake 3D animation as on this site: http://finnrudolph.de/JavaScriptObject/ Or
Suppose I created index with descending order CREATE INDEX `MyTable.MyIndex` USING BTREE ON `MyTable`
When using Redis in order to create a record you can create a hash
Right now I'm using JFreeChart in order to create a dynamic chart. However the
I'm trying to convert datetime2 to datetime in order to create a standard between
I'm using the openssl utility of debian in order to create a certificate. The
I'm using the following script in my website in order to create pagination next-previous
could someone please tell me what I need to do in order to create

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.