I'm currently trying to port some code over from a system that uses Linq to SQL to Entity Framework Core 2.0. The below code is working in Linq to SQL however doesn't work in 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.
Yes I get an exception when the 1st loqs.Count is called:
{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.
If I add the line:
where c3.tnMobileWeight != null
after the 2nd where statement - it all works as it should.
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) { }
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.