Question How to set field with another one

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
362
Programming Experience
10+
Hello,

I have this OrderDetail class. I want to set the Currency field to ExtendedCurrency. Can I set it in the LINQ query below? Or is it possible to set it in the class?

Thank you.

C#:
public async Task<IEnumerable<Order?>> GetAllOrders()
{
   return await _db.Orders.Include(d => d.OrderDetails).ThenInclude(v => v.Vendor).ToListAsync();
}

C#:
public class OrderDetail
    {
        public int Id { get; set; }
        
        [Required]
        [MaxLength(100)]
        public string ProductCode { get; set; }
        [Required]
        [MaxLength(250)]
        public string ProductName { get; set; }
        [Required]
        public int BuyQuantity { get; set; }
        [Required]
        public int SellQuantity { get; set; }
        public double CostRatio { get; set; }
        public double UnitCost { get; set; }
        public double TotalBuyPrice { get; set; }
        public double TotalSellPrice { get; set; }
        [MaxLength(150)]
        public string ShippingNumber { get; set; }
        public string Status { get; set; }
        [MaxLength(150)]
        public string TrackingNumber { get; set; }
        [MaxLength(400)]
        public string Description { get; set; }

        public string Currency { get; set; }
        public int OrderId { get; set; }
        public int VendorId { get; set; }
        public Order Order { get; set; }
        public Vendor Vendor { get; set; }
        [NotMapped] public string? ExtendedCurrency { get; set; }

    }
 
This is just my opinion, but I think the OP is trying to create a View Model only representation of an actual Model field. I think he wants the property to be modifiable for UI purposes, but the modifications do not write back into the Model. See the other thread that I referenced.
 
Good point that (and if it's not what the OP is doing, perhaps it should be)

Sure you can get away with re-using your entities in Blazor Server more easily because the data doesn't leave the server (so you aren't accidentally serializing DB specific stuff out to the world) but reusing the ents usually ends up a messy operation anyway; one is passing around an entity that probably isn't being tracked by the context they attempt to save it with, so there is either some attachment fudging, or there is an attempt to make a context live for an inappropriately long time, or there is a "download, copy data over, save the downloaded ent" process.. nd especially in this latter case one might as well have used a ViewModel after all..

Given that viewmodels and mappers are easily enough generated/can be auto'd for the most part there isn't much argument to staying away from them, I find, but having the separation helps head off confusion across application boundaries..
 
Last edited:

Latest posts

Back
Top Bottom