C
C#•5mo 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•5mo 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
🙈
🙈•5mo 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•5mo 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
🙈
🙈•5mo 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•5mo ago
You want to .OrderBy() the customers, by the .Sum() of their order costs, I would assume
🙈
🙈•5mo ago
but then how do i only return the max like the highest paying
Jimmacle
Jimmacle•5mo ago
how do you think you could achieve that after you put them in order?
🙈
🙈•5mo ago
um i tryed this salesData.OrderBy(z => z.orders.userId).Sum(z => z.orders.totalprice).max()
Jimmacle
Jimmacle•5mo 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?
🙈
🙈•5mo 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•5mo 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
🙈
🙈•5mo ago
i want the actually cusotmer @jIMMACLE
Jimmacle
Jimmacle•5mo ago
then it makes more sense to query the customer set instead of whatever salesData is
🙈
🙈•5mo ago
ok so um but i am doing that right sorry i am new to this
Jimmacle
Jimmacle•5mo ago
we have no idea what salesData is, so i can't tell you
🙈
🙈•5mo ago
oh it is a var that jsut contains orderedfood
Jimmacle
Jimmacle•5mo ago
which is...? that doesn't sound like a set of customers to me
🙈
🙈•5mo ago
my bad i shoud be using orderdata which is orderdata.userId orderdata.total price
Jimmacle
Jimmacle•5mo 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
🙈
🙈•5mo ago
i Just dont get the queery could you type the query and what you ment by sort by userid
Jimmacle
Jimmacle•5mo 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
🙈
🙈•5mo ago
yes that is excatly what i have
Jimmacle
Jimmacle•5mo ago
dbContext.Users.OrderByDescending(u => u.Orders.Sum(o => o.TotalPrice)).First()
🙈
🙈•5mo ago
oh ok got it thank you so much i thoguh i had to use max or somthing
Jimmacle
Jimmacle•5mo ago
zzzz told you exactly what i just spelled out 2 hours ago 😛
🙈
🙈•5mo ago
oh lol could i ask you some mroe stuff about querrys if you are free
Jimmacle
Jimmacle•5mo ago
you can ask here, i might answer if i'm still on
🙈
🙈•5mo 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•5mo ago
you should look at the methods available to you in LINQ specifically Take
🙈
🙈•5mo 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•5mo ago
give it a try and see how far you get
🙈
🙈•5mo 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•5mo ago
it depends but probably i don't have enough context to tell you
🙈
🙈•5mo 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•5mo 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
🙈
🙈•5mo ago
its fine ig i will try improve tommrow i will go sleep thank you very much
Jimmacle
Jimmacle•5mo ago
or just share some code?
🙈
🙈•5mo ago
tommrow thank you