Home Upgrade Search Memberlist Extras Hacker Tools Award Goals Help Wiki Contact

C# 

ASP.NET MVC LINQ Help Needed

I am still new to ASP.NET as a language when it comes to programming.

I am stuck on how to correctly write a LINQ statement that will select all the results from a database but only matching the user input from the dropdown list and then display each row of data out in a table.

I will include a screenshot of what I have... (I know it is not right and not the best but I'm looking for someone who can help me clean it up and get it working since I know it is probably a really simple select statement to write.)

[Image: fmTYILL.png]


If you think you could help out post below and I will either PM you or feel free to add me on discord since it will be easier for me to send screenshots and explain exactly what I need it to do.

Add me on discord: JakeJohnson#9640

Thanks
[Image: jake-johnson-hf-sig.png]
Use the Pomelo.EntityFrameworkCore.MySql package on Nuget and set up in your Startup.cs file, then your query would be something like ->

Code
var authors = _context.Authors.Where(x => x.AuthorNum == uAuth);

if you need further assistance setting this up let me know :)
Inactive, I don't sell products anymore. Please do not message me.
Just here to occasionally lurk.
(Dec 8, 2020 07:36 PM)Jake Johnson Wrote:  I am still new to ASP.NET as a language when it comes to programming.

I am stuck on how to correctly write a LINQ statement that will select all the results from a database but only matching the user input from the dropdown list and then display each row of data out in a table.

I will include a screenshot of what I have... (I know it is not right and not the best but I'm looking for someone who can help me clean it up and get it working since I know it is probably a really simple select statement to write.)

[Image: fmTYILL.png]


If you think you could help out post below and I will either PM you or feel free to add me on discord since it will be easier for me to send screenshots and explain exactly what I need it to do.

Add me on discord: JakeJohnson#9640

Thanks

There's very important aspect that you have the opportunity to learn here.

Code
var allbooks = db.BOOKs.ToList();

This line will actually return all the books from your data source to your application. Imagine the code and the datasource are on separate servers and you have a list of 1m books that you've imported from somewhere. In the code snippet we have here you would be transferring all the info the program and then searching through all the results.

Ideally in a relational database world you'd want to make sure you have a covering index on the database table and you have the search being performed there, something closer the answer provided by KasprDev.

Code
var authors = _context.Authors.Where(x => x.AuthorNum == uAuth);

This will send the uAuth value to the DB and only return the correct results.

I'd highly suggest that while you are here, you take a look at IEnumerable vs IQueryable. There's a great article about this over at codeproject > https://www.codeproject.com/articles/732...iqueryable
The replies above mine cover your question, so I am not gonna write the same things thrice. Nevertheless, since you are using VS, considering installing the ReSharper extension by JetBrains. It automatically converts loops to nice and compact LINQ expressions with one-click. Very handy, especially when you want to see examples of how the engine turns often complicated loops to simplistic and chained expressions.


Possibly Related Threads...
Thread / Author Replies Views Last Post
[.NET] by iconv32Jul 10, 2022 12:18 PM 0 211 Jul 10, 2022 12:18 PM
Last Post: iconv32
[C#] by SouthSide1990Nov 16, 2020 05:48 PM 0 218 Nov 16, 2020 05:48 PM
Last Post: SouthSide1990
[C#] by Jake JohnsonOct 28, 2020 10:15 AM 1 189 Oct 28, 2020 10:15 AM
Last Post: itcorp