Linq группируется в Entity Framework - PullRequest
0 голосов
/ 26 мая 2018

В настоящее время я пытаюсь перенести некоторый код из системы, которая использует Linq для SQL, на Entity Framework Core 2.0.Приведенный ниже код работает в Linq to SQL, но не работает в EF.

Что я делаю не так?

int lnScore1 = 0;
int lnScore2 = 0;
int lnScore3 = 0;

try
{
    var loqs = from r in _context.tblemployee_incidents
               join c in _context.tblemployees on r.diEmployeeID equals c.diID
           join c3 in _context.tbl_config_event_categories on r.dnCategory3 equals c3.tiID into j7
           from c3 in j7.DefaultIfEmpty()
           where c.dbDeleted == false
           where r.diAppID == 1 && r.dbDeleted == false
           group c3 by new { c.diID } into g
           select new
           {
               dnEmployee = g.Key.diID,
               dnWeight = g.Sum(ity => ity.tnMobileWeight)
           };

    lnScore1 = loqs.Count(x => x.dnEmployee > 0 && (x.dnWeight > 0 && x.dnWeight < 30));
    lnScore2 = loqs.Count(x => x.dnEmployee > 0 && (x.dnWeight >= 30 && x.dnWeight < 40));
    lnScore3 = loqs.Count(x => x.dnEmployee > 0 && (x.dnWeight >= 40));
}
catch (Exception ex) { }

Спасибо!

@ DavidG Извините, япредназначено для добавления ошибки.

Да, я получаю исключение при вызове 1-го loqs.Count:

{System.NullReferenceException: Object reference not set to an instance of an object.
at lambda_method(Closure , tbl_config_event_categories )
at System.Linq.Enumerable.SelectIListIterator`2.MoveNext()
at System.Linq.Enumerable.Sum(IEnumerable`1 source)
at lambda_method(Closure , IGrouping`2 )
at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.GetCount(Boolean onlyIfCheap)
at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
at lambda_method(Closure , QueryContext )
at Microsoft.EntityFrameworkCore.Query.Internal.QueryCompiler.<>c__DisplayClass17_1`1.<CompileQueryCore>b__0(QueryContext qc)
at System.Linq.Queryable.Count[TSource](IQueryable`1 source, Expression`1 predicate)
at T9.Web.Controllers.HomeController.Index()

1 Ответ

0 голосов
/ 26 мая 2018

Я наконец понял - не знаю, почему это нормально в LinqPad и Linq to SQL, но не получается в EF .Net Core.

Если я добавлю строку:

                       where c3.tnMobileWeight != null

после 2-го оператора where - все работает как надо.

т.е.:

int lnScore1 = 0;
int lnScore2 = 0;
int lnScore3 = 0;
try
{
    var loqs = from r in _context.tblemployee_incidents
           join c in _context.tblemployees on r.diEmployeeID equals c.diID
       join c3 in _context.tbl_config_event_categories on r.dnCategory3 equals c3.tiID into j7
       from c3 in j7.DefaultIfEmpty()
       where c.dbDeleted == false
       where r.diAppID == 1 && r.dbDeleted == false
       where c3.tnMobileWeight != null
       group c3 by new { c.diID } into g
       select new
       {
           dnEmployee = g.Key.diID,
           dnWeight = g.Sum(ity => ity.tnMobileWeight)
       };

    lnScore1 = loqs.Count(x => x.dnEmployee > 0 && (x.dnWeight > 0 && x.dnWeight < 30));
    lnScore2 = loqs.Count(x => x.dnEmployee > 0 && (x.dnWeight >= 30 && x.dnWeight < 40));
    lnScore3 = loqs.Count(x => x.dnEmployee > 0 && (x.dnWeight >= 40));
}
catch (Exception ex) { }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...