0

The suggestion above took await System.Threading.Tasks but it still are not displaying my data. Now I got some NullReferenceException: Object reference not set to an instance of an object. from my QueryableMappingExtensions. Not sure why?

namespace ArtGallery.Core.Mapping
{
    using System;
    using System.Linq;
    using System.Linq.Expressions;
    using AutoMapper.QueryableExtensions;

    public static class QueryableMappingExtensions
    {
        public static IQueryable<TDestination> To<TDestination>(
            this IQueryable source,
            params Expression<Func<TDestination, object>>[] membersToExpand)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return source.ProjectTo(AutoMapperConfig.MapperInstance.ConfigurationProvider, null, membersToExpand);
        }

        public static IQueryable<TDestination> To<TDestination>(
            this IQueryable source,
            object parameters)
        {
            if (source == null)
            {
                throw new ArgumentNullException(nameof(source));
            }

            return source.ProjectTo<TDestination>(AutoMapperConfig.MapperInstance.ConfigurationProvider, parameters);
        }
    }
}

Full Error Message

NullReferenceException: Object reference not set to an instance of an object.

This error means that I am trying to access something that is null or does not exist, but I am not sure why is it that as I got the Entity in my database and it is with some inserted data from my Seeder.

This is the data in my Database: enter image description here

  • The `ArgumentNullException`, despite well intended, would not be raised when executed as an extension method, because before that the `NullReferenceException` would be raised instead. The code `myClient.To(objParameters)` if `myClient == null` will raise a `NullReferenceException` – Cleptus Apr 20 '22 at 13:38
  • I understand that 'NullReferenceException' is raised before 'ArgumentNullException' when executed as an extension but I still was not able to fix it. – Stanislava Stoeva Apr 21 '22 at 23:27
  • Because you can't **if that code is called as an extension method**. If that code is called as an `static` one it would work as you want. Something similar to `IQueryable destinations = QueryableMappingExtensions.To(mySource, whateverParameters);`. But... Obviously you would lose the benefit of the extension method syntax. – Cleptus Apr 22 '22 at 05:59

0 Answers0