No query results
I've run this query in Linqpad7 and it says the query is successful - which it is in my VS2022 web app. However, nothing shows in the results. I've tried .Dump(); in a couple of places - but still no results. I can run a "take 100" and that shows a correct result, so I know the DB is connected. The query is using daveBlogContext in daveBlog.dll. No errors are showing in Linkpad.
So, what do I need to do?
void Main()
{
}
namespace daveBlog.Pages
{
public class FAQlistModel : PageModel
{
private readonly daveBlog.Data.daveBlogContext _context;
public FAQlistModel(daveBlog.Data.daveBlogContext context) { _context = context; } public IList<myPosts> myPosts { get; set; } = default!; public async Task OnGetAsync() { if (_context.myPosts != null) { var myposts = await _context.myPosts.Where (x => x.IsApproved == "FAQ"). OrderBy(x => x.PublishDate). Select (x => new myPosts { Id = x.Id, Title = x.Title, Summary = x.Summary, Content = x.Content.Substring(0, 200), IsApproved = x.IsApproved, IsReader = x.IsReader, PublishDate = x.PublishDate, ReaderName = x.ReaderName, ReaderEmail = x.ReaderEmail }).ToListAsync(); myPosts = myposts; } } }
}
Comments
-
There's nothing inside your Main method.
-
I'm new to linqpad so can you give me an idea of what I should put in there. This is a Razor Pages app. Thanks
-
I can't figure out what to put in void Main(){} Other people using VS Razor must have come up with some solution?
This was my # 1 reason for buying the Premium version. It seems like most of the pieces are in place. -
Is there another way to test the query itself against my database without using my entire Razor page?
-
A little hard to understand why this forum couldn't provide a few clues to point me in the right direction. In any event, I found enough info in the help-samples to get a useful Results listing and to now have a"scratch pad" for testing queries against my database:
var dataContext = this;
var query = await dataContext.myPosts.Where
(x => x.IsApproved == "News").
OrderBy(x => x.PublishDate).
Select
(x => new daveBlog.Models.myPosts
{
Id = x.Id,
Title = x.Title,
Summary = x.Summary,
Content = x.Content.Substring(0,30),
IsApproved = x.IsApproved,
IsReader = x.IsReader,
PublishDate = x.PublishDate,
ReaderName = x.ReaderName,
ReaderEmail = x.ReaderEmail}).ToListAsync(); query.Dump();
-
You can simplify this further:
myPosts .Where(x => x.IsApproved == "News") .OrderBy (x => x.PublishDate) .Select(x => new daveBlog.Models.myPosts { Id = x.Id, Title = x.Title, Summary = x.Summary, Content = x.Content.Substring (0, 30), IsApproved = x.IsApproved, IsReader = x.IsReader, PublishDate = x.PublishDate, ReaderName = x.ReaderName, ReaderEmail = x.ReaderEmail }).Dump();
And if you change the Query Language to C# Expression, you can even drop the call to Dump() and the semicolon:
myPosts .Where(x => x.IsApproved == "News") .OrderBy (x => x.PublishDate) .Select(x => new daveBlog.Models.myPosts { Id = x.Id, Title = x.Title, Summary = x.Summary, Content = x.Content.Substring (0, 30), IsApproved = x.IsApproved, IsReader = x.IsReader, PublishDate = x.PublishDate, ReaderName = x.ReaderName, ReaderEmail = x.ReaderEmail })