efcore: On Query cases: Microsoft.EntityFrameworkCore.dll: 'Value cannot be null.'

Repro repository https://github.com/Jonatthu/efcore-examples

On line 120 of program.cs and line 130 as well.

// Is not working
county = dbContext.User
              .Select(lambda)
	      .Where(x => 
	         new []{ 44, 5, 546, 99 }.Contains(x.Blogs.Count())
	      )
              .ToList();

The first 2 examples are workarounds but only will generate more request to the database, it should work with the examples below those.

I have included console logs for the queries that are being generated. The repro should work as soon as you clone it a hit f5 on vscode or vs. 😃

Exception has occurred: CLR/System.ArgumentNullException
Exception thrown: 'System.ArgumentNullException' in Microsoft.EntityFrameworkCore.dll: 'Value cannot be null.'
   at System.Linq.Enumerable.Count[TSource](IEnumerable`1 source)
   at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext()
   at Microsoft.EntityFrameworkCore.Query.Internal.LinqOperatorProvider.ExceptionInterceptor`1.EnumeratorExceptionInterceptor.MoveNext()
   at System.Collections.Generic.List`1.AddEnumerable(IEnumerable`1 enumerable)
   at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source)
   at ConsoleApp4.Program.Main(String[] args) in /Users/jonatthu/Documents/Jonatthu/efcore-examples/ConsoleApp4/Program.cs:line 121

About this issue

  • Original URL
  • State: closed
  • Created 6 years ago
  • Comments: 20 (9 by maintainers)

Most upvoted comments

It can be done using single query.

var result = db.Users.Where(u => u.Blogs.Count() > 10 && u.Friends.Count() > 20)
                       .Select(u => new { u.Username, u.Birthday })
                      .ToList()

Which should generate SQL like

SELECT [u].[Username], [u].[Birthday]
FROM [Users] AS [u]
WHERE (
    SELECT COUNT(*)
    FROM [Blogs] AS [b]
    WHERE [u].[Id] = [b].[UserId]) > 10 AND (
    SELECT COUNT(*)
    FROM [Friends] AS [f]
    WHERE [u].[Id] = [f].{UserId]) > 20