C
C#•11mo ago
🙈

create a ef core qurry to find the highest paying cuostmer.

Var CoolCusotmer = DataDetials.Max.Where(a.cusid = a.cusid).sum(totalorderprice)
38 Replies
mg
mg•11mo ago
you're going to have to write out a question with some detail if you want an answer by the looks of it it seems like what you want is to get the sum of the order prices grouped by customer id, and then select the top one
🙈
🙈OP•11mo ago
yes order tabel as cusotmer id and totlat price of order in ti. So i jsut want to find the cusotmer that spent the most
mg
mg•11mo ago
https://learn.microsoft.com/en-us/ef/core/querying/complex-query-operators#groupby this should get you where you want to go like i said, you want to get the sum of the order prices grouped by customer, and then get the top one
🙈
🙈OP•11mo ago
i kinda already looked t that i dont get how to put ti togther i knwo i haveto use .max a.where and a sum but dont know how
Angius
Angius•11mo ago
You want to .OrderBy() the customers, by the .Sum() of their order costs, I would assume
🙈
🙈OP•11mo ago
but then how do i only return the max like the highest paying
Jimmacle
Jimmacle•11mo ago
how do you think you could achieve that after you put them in order?
🙈
🙈OP•11mo ago
um i tryed this salesData.OrderBy(z => z.orders.userId).Sum(z => z.orders.totalprice).max()
Jimmacle
Jimmacle•11mo ago
what is ordering by user ID doing for you? would it be more helpful to order by something else? maybe something you want to get the highest one of?
🙈
🙈OP•11mo ago
i want to get the highesh paying cusotmer by suming the total price of all thier orders highest sum of total prices where the cusotmer id is the same
Jimmacle
Jimmacle•11mo ago
do you only want the sum or do you want the actual customer? either way i would start the query from the customers set, then assuming you have a navigation property for their orders go from there
🙈
🙈OP•11mo ago
i want the actually cusotmer @jIMMACLE
Jimmacle
Jimmacle•11mo ago
then it makes more sense to query the customer set instead of whatever salesData is
🙈
🙈OP•11mo ago
ok so um but i am doing that right sorry i am new to this
Jimmacle
Jimmacle•11mo ago
we have no idea what salesData is, so i can't tell you
🙈
🙈OP•11mo ago
oh it is a var that jsut contains orderedfood
Jimmacle
Jimmacle•11mo ago
which is...? that doesn't sound like a set of customers to me
🙈
🙈OP•11mo ago
my bad i shoud be using orderdata which is orderdata.userId orderdata.total price
Jimmacle
Jimmacle•11mo ago
can you share your whole data model? i don't know if it's the naming or what but i don't think you're understanding what i'm suggesting to do
🙈
🙈OP•11mo ago
i Just dont get the queery could you type the query and what you ment by sort by userid
Jimmacle
Jimmacle•11mo ago
so if i had users with orders with prices and i wanted to get the user with the highest total value of orders it would look something like
🙈
🙈OP•11mo ago
yes that is excatly what i have
Jimmacle
Jimmacle•11mo ago
dbContext.Users.OrderByDescending(u => u.Orders.Sum(o => o.TotalPrice)).First()
🙈
🙈OP•11mo ago
oh ok got it thank you so much i thoguh i had to use max or somthing
Jimmacle
Jimmacle•11mo ago
zzzz told you exactly what i just spelled out 2 hours ago 😛
🙈
🙈OP•11mo ago
oh lol could i ask you some mroe stuff about querrys if you are free
Jimmacle
Jimmacle•11mo ago
you can ask here, i might answer if i'm still on
🙈
🙈OP•11mo ago
ight so say i want to reutrn a list of the top 5 or so what ever like we have frist how would i do that
Jimmacle
Jimmacle•11mo ago
you should look at the methods available to you in LINQ specifically Take
🙈
🙈OP•11mo ago
ok thank you and finaly if i have a stock stock stored in the food tableand have a orderitem mdoel which contains foodid and quanity how would i say when user clciks button to check where the foodid matchs and minius the quanity form the stock is this hard to achive
Jimmacle
Jimmacle•11mo ago
give it a try and see how far you get
🙈
🙈OP•11mo ago
ight i am might take m e some time thank you for the help i got it working wiht a nested for loop its that bad pratice
Jimmacle
Jimmacle•11mo ago
it depends but probably i don't have enough context to tell you
🙈
🙈OP•11mo ago
what would be a better way the way i did it was for every item that exist we go into the next for loop and check for each of the items in the order macths wiht the food id
Jimmacle
Jimmacle•11mo ago
i don't know how you actually did it, you haven't shown any code you just told me you used nested loops i'm not a mind reader
🙈
🙈OP•11mo ago
its fine ig i will try improve tommrow i will go sleep thank you very much
Jimmacle
Jimmacle•11mo ago
or just share some code?
🙈
🙈OP•11mo ago
tommrow thank you
Want results from more Discord servers?
Add your server