I have a database table that has a series of notifications. There can be multiple notifications for the same city, xgrid and ygrid. So I am getting the latest date and using that record via the SQL Lite query.
After reading through several articles on this site I have tried to piece together the Model Query equivalent to the SQL Lite query below.
The Problem is the Model Query has no compilation errors, but the results are null.
SQL Lite Query
select * from (
select
NOTIFY_ID,
NOTIFICATION_DATE,
CITY_NAME,
ITEM_X_GRID,
ITEM_Y_GRID,
GRID_QUANTITY,
row_number() over(partition by ITEM_X_GRID, ITEM_Y_GRID, CITY_NAME order by NOTIFICATION_DATE desc) as rn
from
USER_ILLY_DATA
) t
where t.rn = 1
order by CITY_NAME
Class & List
public class MostRecentNotify
{
public int RecordID { get; set; }
public string ItemXGrid { get; set; }
public string ItemYGrid { get; set; }
public string GridQuantity { get; set; }
public string NotificationDate { get; set; }
public string CityName { get; set; }
public string IllyItemCode { get; set; }
}
public IList<MostRecentNotify> RecentNotifies { get; set; }
Model Query To Get Latest Record (not working)
var tempResults = _context.IllyAPIData.GroupBy(i => new { i.ItemXGrid, i.ItemYGrid, i.CityName})
.Select(g => g.OrderByDescending(y => y.NotificationDate).FirstOrDefault());
Model Query to Pass values to Class to be called in Razor Pages
var RecentNotifies = tempResults.Select(r => new MostRecentNotify//).ToListAsync();
{
ItemXGrid = r.ItemXGrid,
ItemYGrid = r.ItemYGrid,
GridQuantity = r.GridQuantity,
NotificationDate = r.NotificationDate,
CityName = r.CityName,
IllyItemCode = r.IllyriadCode,
RecordID = r.RecordID,
}).ToListAsync();
Razor Pages snippet
@foreach (var item in Model.RecentNotifies.Where(i => i.CityName == city.DistinctCityName)){
@foreach (var indRes in Model.RareResources.Where(r => r.ResourceCode == item.IllyItemCode)){
Error on Page Load
ArgumentNullException: Value cannot be null. (Parameter 'source')
System.Linq.ThrowHelper.ThrowArgumentNullException(ExceptionArgument argument)
System.Linq.Enumerable.Where<TSource>(IEnumerable<TSource> source, Func<TSource, bool> predicate)
IllyriadAssist.Pages.harvestableInventory.Pages_harvestableInventory_Index.ExecuteAsync() in Index.cshtml
+ @foreach (var item in Model.RecentNotifies.Where(i => i.CityName == city.DistinctCityName)){
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageCoreAsync(IRazorPage page, ViewContext context)
Microsoft.AspNetCore.Mvc.Razor.RazorView.RenderPageAsync(IRazorPage page, ViewContext context, bool invokeViewStarts)
After a bit more poking around I was able to get the queries to work by doing it this way:
var tempData = from c in _context.IllyAPIData
group c by new { c.CityName, c.ItemXGrid, c.ItemYGrid } into g
select new
{
g.Key.CityName,
g.Key.ItemXGrid,
g.Key.ItemYGrid,
NotifyDate = g.Max(a => a.NotificationDate)
};
var RecentNotifies = (from c in _context.IllyAPIData
join s in tempData
on new { c.CityName, c.ItemXGrid, c.ItemYGrid }
equals new { s.CityName, s.ItemXGrid, s.ItemYGrid }
where c.NotificationDate == s.NotifyDate
select c).ToListAsync();
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.