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