C
C#β€’11mo ago
πŸ™ˆ

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
Angius
Angiusβ€’11mo ago
With EF, presumably
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
yes but how
Angius
Angiusβ€’11mo ago
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?
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
na i cant cus it is a lsit/collection i cant jsut be like viewbag. =
Angius
Angiusβ€’11mo ago
What does it being a collection have anything to do with anything?
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
idk i am new to this
Angius
Angiusβ€’11mo ago
var stuff = await _context.Things.ToListAsync();
return View(stuff);
var stuff = await _context.Things.ToListAsync();
return View(stuff);
Boom Done List passed to the view
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
ill show you my code one sec but int eh view ig like for each var i in stuff \
Angius
Angiusβ€’11mo ago
But what the what the what?
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
it will say stuff doesnt exist
Angius
Angiusβ€’11mo ago
@Model List<Thing>

@foreach (var thing in Model)
{
<p>@thing.Bla</p>
}
@Model List<Thing>

@foreach (var thing in Model)
{
<p>@thing.Bla</p>
}
?
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
do you think my query will work
Angius
Angiusβ€’11mo ago
Idk what salesData is, but yeah, it should
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
sales data = _context.sales
Angius
Angiusβ€’11mo ago
Dunno why you'd have a variable fot that, but sure Doesn't change anything, query should work
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
in the view
Angius
Angiusβ€’11mo ago
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)
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
um bit confused but i am trying. could you please expalain this line to me
Angius
Angiusβ€’11mo ago
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 property
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
oh 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.
Angius
Angiusβ€’11mo ago
You can make a record or a class, put all the data in there, and pass that to the model
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
like a view model
Angius
Angiusβ€’11mo ago
Ye
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
can i not use vivw bag or is that only for single items.
Angius
Angiusβ€’11mo ago
Avoid ViewBag
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
ight
Angius
Angiusβ€’11mo ago
It's basically a Dictionary<string, object?> so you get rid of any and all type safety
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
ok 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
Unknown Userβ€’11mo ago
Message Not Public
Sign In & Join Server To View
Angius
Angiusβ€’11mo ago
You just make a class like
public class StuffViewModel
{
public required List<Thing> Things { get; init; }
public required string Name { get; init; }
}
public class StuffViewModel
{
public required List<Thing> Things { get; init; }
public required string Name { get; init; }
}
create an instance of it, like any other class
var data = new StuffViewModel {
Things = yourQueryResult,
Name = "some more data"
}
var data = new StuffViewModel {
Things = yourQueryResult,
Name = "some more data"
}
And pass that to the view
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
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
Angius
Angiusβ€’11mo ago
No clue about your data models, but something like
var stuff = await _ctx.Sales
.Where(sale => sale.Id == someId)
.Select(sale => new SaleDto {
Bla = sale.Bla,
Foo = sale.Foo,
Cost = sale.Products.Sum(product => product.Price)
})
.FirstOrDefaultAsync();
var stuff = await _ctx.Sales
.Where(sale => sale.Id == someId)
.Select(sale => new SaleDto {
Bla = sale.Bla,
Foo = sale.Foo,
Cost = sale.Products.Sum(product => product.Price)
})
.FirstOrDefaultAsync();
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
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
Angius
Angiusβ€’11mo ago
Let's format your code first
await _context.Sales
.GroupBy(a => a.ProductidFk)
.OrderBydescending(a => a.Sum(a.Quantiysold))
.TolsitAsync();
await _context.Sales
.GroupBy(a => a.ProductidFk)
.OrderBydescending(a => a.Sum(a.Quantiysold))
.TolsitAsync();
.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 sum
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
thank 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
Angius
Angiusβ€’11mo ago
Group and sum, I guess
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
await _context.Sales .GroupBy(a => a.ProductidFk) .OrderBydescending(a => a.Sum(a.Quantiysold)) .TolsitAsync(); i tried
Angius
Angiusβ€’11mo ago
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
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
but whats do you mean by group it
Angius
Angiusβ€’11mo ago
.GroupBy() sale.Products.GroupBy(product => product.Name) Or w/e else you want to group it by Then you can .Sum() within each group
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
how sorry i am very bad at this
Angius
Angiusβ€’11mo ago
Look up a tutorial/course then A systematic way to learn EF Core will do you better than my ad-hoc help
πŸ™ˆ
πŸ™ˆOPβ€’11mo ago
ight but could you show me this bit if you dont mind how would i sum within each group
MODiX
MODiXβ€’11mo ago
Angius
REPL Result: Success
class ThingDto {
public bool IsEven { get; init; }
public int Sum { get; init; }
public int SumTimesTwo { get; init; }
}

new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
.GroupBy(n => n % 2 == 0)
.Select(g => new ThingDto
{
IsEven = g.Key,
Sum = g.Sum(),
SumTimesTwo = g.Sum(n => n * 2)
})
.ToList()
class ThingDto {
public bool IsEven { get; init; }
public int Sum { get; init; }
public int SumTimesTwo { get; init; }
}

new[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }
.GroupBy(n => n % 2 == 0)
.Select(g => new ThingDto
{
IsEven = g.Key,
Sum = g.Sum(),
SumTimesTwo = g.Sum(n => n * 2)
})
.ToList()
Result: List<ThingDto>
[
{
"isEven": false,
"sum": 25,
"sumTimesTwo": 50
},
{
"isEven": true,
"sum": 30,
"sumTimesTwo": 60
}
]
[
{
"isEven": false,
"sum": 25,
"sumTimesTwo": 50
},
{
"isEven": true,
"sum": 30,
"sumTimesTwo": 60
}
]
Quoted by
<@85903769203642368> from #bot-spam (click here)
Compile: 500.777ms | Execution: 101.872ms | React with ❌ to remove this embed.
Angius
Angiusβ€’11mo ago
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
Want results from more Discord servers?
Add your server