简体   繁体   中英

c# LINQ query to select whole object into new?

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.

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

You should use AutoMapper here to avoid writing each path.

  1. https://automapper.org/
  2. http://docs.automapper.org/en/stable/Getting-started.html

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#

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.

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