Search code examples
c#linqfastmember

Argument out of range exception, Parameter name : name


Each time trying to execute the following code with different rows; getting the same exception :

 var result = (from prod in context.ProductsTbls
                          join img in context.ProductImagesTbls
                          on prod.Id equals img.ProductId
                          where prod.UserId == 4 && img.IsDefaultImage ==true
                          select new
                          {                            
                              Image = img.Image
                          }).ToList();


            IEnumerable<Object> data = result.ToList();
            DataTable table = new DataTable();

            using (var reader = ObjectReader.Create(data , "Image"))
            {
                table.Load(reader); // Exception appears here
            }

And getting this exception :

System.ArgumentOutOfRangeException: 'Specified argument was out of the range of valid values. Parameter name: name'

Please help me regarding this issue.


Solution

  • So you're using a third-party library, Fast-Member which is aimed at dynamically accessing type members way faster than reflection.

    The problem is that you cast your collection to IEnumerable<Object>, so all type information is gone and the member name Image can't be found.

    Just remove this cast, it's useless. You can remove the line IEnumerable<Object> data = result.ToList(); entirely and offer result to ObjectReader.