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; }

    }
 
A query is something that retrieves data. Although people tend to say "SQL query" for any SQL statement(s), only a SELECT statement is a query. LINQ is Language INtegrated Query, so it is a way to retrieve data. If you want to make changes to that data, that's something you do after the query. In your case, you'll basically need to loop through the Order objects and loop through the OrderDetails of each one, setting that property inside the inner loop.
 
If you want to set a field in every OrderDetail then it seems odd to go via the Orders and pull back Vendors as well. Maybe you want to do other things too but, if that's it, you'd be better of getting the OrderDetails directly without anything else.

That said, if you want to update one column in every record in a table to the same value, that's something that would be done more efficiently at the database, without pulling any data back at all. You could do that by executing a SQL script against the database or you can actually execute Entity SQL directly via your DbContext. There is a method named ExecuteSqlRaw or the like that you could call to execute an UPDATE directly instead of pulling lots of data into the application first.
 
Maybe something like this at the class level
[I]public string Currency { get { return ExtendedCurrency;} set { ExtendedCurrency = value; }[/I]
 
I did like this but ExtendedCurrency is null.
C#:
private string _currency;
        public string Currency
        {
            get { return _currency; }
            set
            {
                _currency = value;
                ExtendedCurrency = value;
            }
        }

    
        [NotMapped] public string? ExtendedCurrency { get; set; }
 
You originally said:
I want to set the Currency field to ExtendedCurrency.
which means that you want to assign the ExtendedCurrency property value to the Currency property. The code in post #5 seems to support that but the code in post #7 seems to suggest the opposite, i.e. that you want to assign the Currency property value to the ExtendedCurrency property. Which is it? If you're confused, explain how you envision them working from an application perspective and then we can tell you how to code that.
 
So you want to be able to set ExtendedCurrency independently but, any time Currency is set, you want ExtendedCurrency set to the same value, which would also mean that they have the same value by default when pulling entities from the database? When you say:
I did like this but ExtendedCurrency is null.
do you mean that, immediately after pulling records from the database, the Currency property has a value but the ExtendedCurrency property does not? Did you put a breakpoint on that property setter to see whether it was hit and the code as you expected?
 
So you want to be able to set ExtendedCurrency independently
Actually no need for an independent setting for ExtendedCurrency.
ny time Currency is set, you want ExtendedCurrency set to the same value, which would also mean that they have the same value by default when pulling entities from the database
Totally agree :)
do you mean that, immediately after pulling records from the database, the Currency property has a value but the ExtendedCurrency property does not?
Unfortunately yes, Currency has value but ExtendedCurrency is NULL.
 
Actually no need for an independent setting for ExtendedCurrency.
So what is ExtendedCurrency for then? It seems like you're just creating a read-only copy of Currency.
 
I suspect our OP is going a round about way of trying too solve his problem in his other thread:
 
If you're trying to use "ExtendedCurrency" in code and map that to a column called "Currency" there are simpler ways..
 

Latest posts

Back
Top Bottom