Run Query in asp.net mvc
In ASP.net mvc I have a order by descending query in a controller how would i excute the the query and display the list/reuslts in a view
45 Replies
With EF, presumably
yes but how
And you would return it to the view like any other bit of data
Do you have any code that doesn't work?
Or is it a case of not willing to google/search the docs?
na i cant cus it is a lsit/collection
i cant jsut be like viewbag. =
What does it being a collection have anything to do with anything?
idk i am new to this
Boom
Done
List passed to the view
ill show you my code one sec
but int eh view ig like for each var i in stuff \
But what the what the what?
it will say stuff doesnt exist
?
do you think my query will work
Idk what
salesData
is, but yeah, it shouldsales data = _context.sales
Dunno why you'd have a variable fot that, but sure
Doesn't change anything, query should work
in the view
See this?
The model you pass to the view is called...
Model
The view has no clue about how you named variables in the controller
It only has one thing
Model
Which is a property that holds the stuff you gave it
return View(this_stuff_here)
um bit confused but i am trying.
could you please expalain this line to me
It should be
var item in Model
I just told you the data you pass is stored in the property named Model
What's there to explain?
You call a method that renders the view
And pass your data to it
That's it
That data then gets put in the view's Model
propertyoh ok i see but i actually want to add more querrys and return them all to the same views. Before i was adding single items to the view bag and wodnered if i could do anythign with a list.
You can make a record or a class, put all the data in there, and pass that to the model
like a view model
Ye
can i not use vivw bag or is that only for single items.
Avoid ViewBag
ight
It's basically a
Dictionary<string, object?>
so you get rid of any and all type safetyok so how would i get the data to class do i bass it of the class that is already there
like how would I add my querys to the class and then have like have all the lsit in one view.
sorry i am asking to many questions
Unknown Userβ’11mo ago
Message Not Public
Sign In & Join Server To View
You just make a class like
create an instance of it, like any other class
And pass that to the view
Hi @ZZZZZZZZZZZZZZZZZZZZZZZZZ I am struggling to get the original query working
I will tell you the details the query is based on _context sales. Sales has products products productid(fk) and quantity
What I want to do is return a list of the products and the total sold of each of the products by summing up the quantities where the product id is the same
How would you approach creating a query like this
No clue about your data models, but something like
I donβt get the new bit
ok i see it it becasue we are making a new lsit
out of stuff that is already there
i am getting my code
@ZZZZZZZZZZZZZZZZZZZZZZZZZ here is my the code i had var quantitysoledofeachproduct = await _context.Sales.GroupBy(a => a.ProductidFk) . OrderBydescending(a => a.Sum(a.Quantiysold)).TolsitAsync();
I get a invaild OperationExpectation. Reweite the query in a from that can be transalted or swithc to cliebt evaluation explicity but inserting a call to asEnumerbale, AsasyncaEnumbared, to list Or to ListAsynac
Let's format your code first
.Sum()
takes a lambda function
So what you have here will not compile
If you want to order by a given property of a Sale
, it will be .OrderBy(sale => sale.ThatProperty)
If ThatProperty
is a list, or any other kind of a collection, you can sum it, sure
.OrderBy(sale => sale.ThatProperty.Sum())
Assuming that .ThatProperty
is a collection of something that can be summed
Like a List<int>
or double[]
Chances are, it is not, but rather it's something like a List<Product>
With each product having a .Price
So you can sum those prices
.OrderBy(sale => sale.Products.Sum(product => product.Price))
The new
bit is a projection
A .Select()
will... select only the data you need
Your Sale
for example will not have a TotalPriceOfAllProducts
property
You need to sum the prices of all products to get that
But you can't just project to arbitrary random data, you need some object to project to
Enter DTOs, or Data Transfer Objects
Which are regular objects, whose only role is to store data. No methods, no constructors, no nothing, just plain properties
That DTO can have properties for everything you want to get from the Sale
, and it can also have a TotalPriceOfAllProducts
property where you can store the result of your price sumthank you however i want to sum the quantity which belongs to sales. productidfk also belongs to sales and prdoucts products. How do i make a query that will give the toal number of each product sold. Like product 1 total sold 5 product 2 toals solded 10
sorry for not explaning properly
Group and sum, I guess
await _context.Sales
.GroupBy(a => a.ProductidFk)
.OrderBydescending(a => a.Sum(a.Quantiysold))
.TolsitAsync(); i tried
Not sure if it can be translated to SQL, so worst case scenario fetch the data and group/sum it on the client
And, as I explained, it had no chance to ever work
but whats do you mean by group it
.GroupBy()
sale.Products.GroupBy(product => product.Name)
Or w/e else you want to group it by
Then you can .Sum()
within each grouphow
sorry i am very bad at this
Look up a tutorial/course then
A systematic way to learn EF Core will do you better than my ad-hoc help
ight
but could you show me this bit
if you dont mind
how would i sum within each group
Angius
REPL Result: Success
Result: List<ThingDto>
Quoted by
<@85903769203642368> from #bot-spam (click here)
Compile: 500.777ms | Execution: 101.872ms | React with β to remove this embed.
This one groups the numbers by whether they're even or odd, then sums all the numbers within that group, as well as sums the (number * 2)s