Hack Forums
[C#] ASP.NET MVC LINQ Help Needed - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [C#] ASP.NET MVC LINQ Help Needed (/showthread.php?tid=6126320)



ASP.NET MVC LINQ Help Needed - Jake Johnson - Dec 8, 2020

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


RE: ASP.NET MVC LINQ Help Needed - Angela White - Dec 8, 2020

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 :)


RE: ASP.NET MVC LINQ Help Needed - Shadow_Cloud - Dec 10, 2020

(Dec 8, 2020 07:36 PM)JakeFPS 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/732425/ienumerable-vs-iqueryable


RE: ASP.NET MVC LINQ Help Needed - Xamarin - Dec 10, 2020

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.