简体   繁体   English

使用实体框架将 SQL Lite 查询转换为 Model 查询时遇到问题

[英]Trouble converting SQL Lite Query to Model Query with Entity Framework

I have a database table that has a series of notifications.我有一个包含一系列通知的数据库表。 There can be multiple notifications for the same city, xgrid and ygrid.同一个城市可以有多个通知,xgrid 和 ygrid。 So I am getting the latest date and using that record via the SQL Lite query.因此,我通过 SQL Lite 查询获取最新日期并使用该记录。

After reading through several articles on this site I have tried to piece together the Model Query equivalent to the SQL Lite query below.在阅读了本网站上的几篇文章后,我试图拼凑出与下面的 SQL Lite 查询等效的 Model 查询。

The Problem is the Model Query has no compilation errors, but the results are null.问题是 Model 查询没有编译错误,但结果是 null。

SQL Lite Query SQL 精简版查询

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 Class & 列表

        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) Model 查询获取最新记录(不工作)

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 Model 查询以将值传递给 Class 以在 Razor 页面中调用

            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 Razor 页面片段

@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();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM