-
-
Notifications
You must be signed in to change notification settings - Fork 231
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
Comments
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 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();
|
Thanks. |
New NuGet will be released soon. |
Thanks! |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The text was updated successfully, but these errors were encountered: