Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BUG: Dynamic new in Where() causes NRE #130

Closed
xumix opened this issue Dec 19, 2017 · 5 comments
Closed

BUG: Dynamic new in Where() causes NRE #130

xumix opened this issue Dec 19, 2017 · 5 comments
Assignees

Comments

@xumix
Copy link

xumix commented Dec 19, 2017

 var keys = context.Model.FindEntityType(type).FindPrimaryKey().Properties;
            var properties = keys.Select(p => new DynamicProperty(p.PropertyInfo.Name, p.PropertyInfo.PropertyType)).ToList();
            var keyType = DynamicClassFactory.CreateType(properties, false);
           
            var dbSet = context.Set<SomeEntity>();
            
            var keyVals = (IList)ReflectionHelper.CreateGenericInstance(typeof(List<>), new[] { keyType });

            foreach (var obj in data)
            {
                var keyVal = Activator.CreateInstance(keyType);
                foreach (var key in keys)
                {
                    keyType.GetProperty(key.PropertyInfo.Name).SetValue(keyVal, key.PropertyInfo.GetValue(obj));
                }

                keyVals.Add(keyVal);
            }
            
            Func<string> getNew = () =>
            {
                var res = keys.Select(key => "it." + key.Name + " as " + key.Name).ToList();
                return "new(" + res.ToString(",") + ")";
            };

            Func<string> getContains = () => ".Contains(" + getNew() + ")";

            var existing = dbSet.Where("@0" + getContains(), keyVals)
                .Select(getNew());
System.ArgumentNullException
  HResult=0x80004003
  Message=Value cannot be null.
  Source=System.Linq.Expressions
  StackTrace:
   at System.Linq.Expressions.Expression.Bind(MemberInfo member, Expression expression)
   at System.Linq.Dynamic.Core.ExpressionParser.CreateNewExpression(List`1 properties, List`1 expressions, Type newType) in C:\Projects\System.Linq.Dynamic.Core\src\System.Linq.Dynamic.Core\ExpressionParser.cs:line 1491

@StefH
Copy link
Collaborator

StefH commented Dec 24, 2017

I'm not able to reproduce this issue. Can you please write working demo code based on https://github.com/StefH/System.Linq.Dynamic.Core/blob/master/src-console/ConsoleAppEF2.0/Program.cs ?

@StefH StefH self-assigned this Jan 4, 2018
@xumix
Copy link
Author

xumix commented Jan 9, 2018

@StefH here we go:

object CreateGenericInstance(Type type, Type[] types, params dynamic[] ctorParams)
        {
            var genType = type.MakeGenericType(types);

            var ct = genType.GetConstructors().First();
            return ct.Invoke(ctorParams);
        }


var keys = context.Model.FindEntityType(typeof(Car)).FindPrimaryKey().Properties;
            var properties = keys.Select(p => new DynamicProperty(p.PropertyInfo.Name, p.PropertyInfo.PropertyType)).ToList();
            var keyType = DynamicClassFactory.CreateType(properties, false);
           
            var dbSet = context.Set<Car>();
            
            var keyVals = (IList)CreateGenericInstance(typeof(List<>), new[] { keyType });

            foreach (var obj in context.Cars.ToArray())
            {
                var keyVal = Activator.CreateInstance(keyType);
                foreach (var key in keys)
                {
                    keyType.GetProperty(key.PropertyInfo.Name).SetValue(keyVal, key.PropertyInfo.GetValue(obj));
                }

                keyVals.Add(keyVal);
            }
            
            Func<string> getNew = () =>
            {
                var res = keys.Select(key => "it." + key.Name + " as " + key.Name).ToList();
                return "new(" + string.Join(",", res) + ")";
            };

            Func<string> getContains = () => ".Contains(" + getNew() + ")";

            var existing = dbSet.Where("@0" + getContains(), keyVals).Select(getNew()).ToDynamicList();

@StefH
Copy link
Collaborator

StefH commented Jan 9, 2018

Thanks.

@StefH StefH changed the title Dynamic new in Where() causes NRE BUG: Dynamic new in Where() causes NRE Jan 9, 2018
StefH added a commit that referenced this issue Jan 9, 2018
@StefH StefH closed this as completed Jan 9, 2018
@StefH
Copy link
Collaborator

StefH commented Jan 9, 2018

New NuGet will be released soon.

StefH added a commit that referenced this issue Jan 9, 2018
* Solved issue #130

* 1.0.8.2
@xumix
Copy link
Author

xumix commented Jan 9, 2018

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

2 participants