2

Not sure if i worded the question correctly, but what im trying to do is return a new viewmodel with one of the parts being a booking:

public class Booking
    {

        public int BookingId { get; set; }
        public int CustomerId { get; set; }

        public Guid UniqueId { get; set; }
        public string EventId { get; set; }
        public bool IsPaid { get; set; }
        public double Price { get; set; }
        public DateTime BookingDate { get; set; }
        public DateTime DateBooked { get; set; }

        [JsonIgnore]
        public Customer Customer { get; set; }

        [JsonIgnore]
        public ICollection<BookingService> BookingServices { get; set; }

        [NotMapped]
        public IEnumerable<Service> Services { get; set; }

    }

and my query is:

            var customers = _dbContext.Customers
                .Select(c => new CustomerBookingsViewModel
                {
                    Customer = c,

                    Bookings = c.Bookings.Select(b => new Booking
                    {
                        BookingId = b.BookingId,
                        BookingDate = b.BookingDate,
                        DateBooked = b.DateBooked,
                        CustomerId = b.CustomerId,
                        UniqueId = b.UniqueId,
                        EventId = b.EventId,
                        IsPaid = b.IsPaid,
                        Price = b.Price,

                        Services = b.BookingServices.Select(s => s.Service)

                    }),
                }
                )
                .ToList();

What I want to know is how to I select all the booking info into the booking without selecting each part, ie:

BookingId = b.BookingId,
BookingDate = b.BookingDate,
DateBooked = b.DateBooked,
CustomerId = b.CustomerId,
UniqueId = b.UniqueId,
EventId = b.EventId,
IsPaid = b.IsPaid,
Price = b.Price,

Can it be done or because the list of services is inside the booking model it cant?

Thanks.

MaxPower
  • 141
  • 1
  • 6
  • why do you need a booking property in the viewmodel? you could instead use `Customer.Booking` , since you already have the booking in the customer that you have mapped – Stucky Dec 10 '19 at 09:16
  • I would have a look at https://automapper.org/ – Johnathan Barclay Dec 10 '19 at 09:25
  • sorry, i should have mentioned the return type is JSON, if I include all the navigation properties the serializer gets cyclic references, so using Customer.Booking doesnt work. – MaxPower Dec 10 '19 at 22:43

4 Answers4

2

You could implement the IClonable interface on your class.

public class MyClass : ICloneable
{
    public int Id { get; set; }
    public object Clone() => MemberwiseClone();
}

Usage:

var list1 = new List<MyClass> 
{ 
    new MyClass() { Id = 2 }, 
    new MyClass() { Id = 5 } 
};

var list2 = list1.Select(x => (MyClass)x.Clone()).ToList();
list2.First().Id = 10; //list1 won't be affected
Innat3
  • 3,561
  • 2
  • 11
  • 29
1

You should use AutoMapper here to avoid writing each path.

  1. https://automapper.org/
  2. http://docs.automapper.org/en/stable/Getting-started.html
Zain Ahmad Khan
  • 477
  • 7
  • 23
0

There is no other way, at least it is not related to LINQ or queries.

The question "How to clone an object" has been answered here: Creating a copy of an object in C#

Holger
  • 2,446
  • 1
  • 14
  • 13
0

There is no LINQ way to do this. I would suggest using custom Attribute marking every property you want to copy. This would help if you want not to copy the whole object but some properties. After marking every property you need you can just set the marked props with reflection from one of the objects to the other.

Iavor Orlyov
  • 512
  • 1
  • 4
  • 15