Question How to set field with another one

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
287
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; }

    }
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,643
Location
Sydney, Australia
Programming Experience
10+
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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,643
Location
Sydney, Australia
Programming Experience
10+
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.
 

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
287
Programming Experience
10+
Maybe something like this at the class level
[I]public string Currency { get { return ExtendedCurrency;} set { ExtendedCurrency = value; }[/I]
 

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
287
Programming Experience
10+
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; }
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,643
Location
Sydney, Australia
Programming Experience
10+
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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,643
Location
Sydney, Australia
Programming Experience
10+
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?
 

raysefo

Well-known member
Joined
Feb 22, 2019
Messages
287
Programming Experience
10+
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.
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
4,643
Location
Sydney, Australia
Programming Experience
10+
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.
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,698
Location
Chesapeake, VA
Programming Experience
10+
I suspect our OP is going a round about way of trying too solve his problem in his other thread:
 

cjard

Well-known member
Joined
Jan 25, 2012
Messages
56
Programming Experience
10+
If you're trying to use "ExtendedCurrency" in code and map that to a column called "Currency" there are simpler ways..
 

Skydiver

Staff member
Joined
Apr 6, 2019
Messages
5,698
Location
Chesapeake, VA
Programming Experience
10+
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.
 

cjard

Well-known member
Joined
Jan 25, 2012
Messages
56
Programming Experience
10+
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:
Top Bottom