简体   繁体   English

在实体框架中的Linq group by

[英]Linq group by in Entity Framework

I'm currently trying to port some code over from a system that uses Linq to SQL to Entity Framework Core 2.0. 我目前正在尝试将一些代码从使用Linq到SQL的系统移植到Entity Framework Core 2.0。 The below code is working in Linq to SQL however doesn't work in EF. 下面的代码在Linq to SQL中有效,但在EF中不起作用。

What am I doing wrong? 我究竟做错了什么?

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) { }

Thanks! 谢谢!

@DavidG Sorry I meant to put in the error. @DavidG对不起,我想输入错误。

Yes I get an exception when the 1st loqs.Count is called: 是的,当第一个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()

I finally figured it out - don't know why it's ok in LinqPad and Linq to SQL but fails in EF .Net Core. 我终于弄清楚了-不知道为什么在LinqPad和Linq to SQL中可以,但是在EF .Net Core中失败。

If I add the line: 如果我添加以下行:

                       where c3.tnMobileWeight != null

after the 2nd where statement - it all works as it should. 在第二条where语句之后-一切正常。

ie: 即:

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) { }

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

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