score:4
the cause of the problem in question is the line
currentparameter = expression.parameter(currenttype, currentparameter.name + "." + property.name);
inside visitnew
method.
with your sample, it creates a new parameter called "x.sub", so if we mark the parameters with {}
, the actual result is
sub = new subtype()
{
id = {x.sub}.id
},
rather than expected
sub = new subtype()
{
id = {x}.sub.id
},
in general you should not create new parameterexpression
s except when remapping lambda expressions. and all newly created parameters should be passed to expression.lambda
call, otherwise they will be considered "not defined".
also please note that the visitor code has some assumptions which doesn't hold in general. for instance
var xoriginal = expression.propertyorfield(currentparameter, x.name);
won't work inside nested new
, because there you need access to a member of the x
parameter like x.sub.id
rather than x.id
. which is basically the corersonding expression from newexpression.arguments
.
processing nested lambda expressions or collection type members and linq methods with expression visitors requires much more state control. while converting simple nested anonymous new
expression like in the sample does not even need a expressionvisitor
, because it could easily be achieved with simple recursive method like this:
public static expression<func<tin, tout>> transform<tin, tout>(this expression<func<tin, object>> source)
{
return expression.lambda<func<tin, tout>>(
transform(source.body, typeof(tout)),
source.parameters);
}
static expression transform(expression source, type type)
{
if (source.type != type && source is newexpression newexpr && newexpr.members.count > 0)
{
return expression.memberinit(expression.new(type), newexpr.members
.select(m => type.getproperty(m.name))
.zip(newexpr.arguments, (m, e) => expression.bind(m, transform(e, m.propertytype))));
}
return source;
}
Source: stackoverflow.com
Related Query
- Variable 'x.Sub' of type 'SubType' referenced from scope '' but it is not defined error
- When mocking Workbook->Worksheets: variable 'p' of type 'Microsoft.Office.Interop.Excel.Workbook' referenced from scope '', but it is not defined
- LINQ error: variable 'x' of type 'y' referenced from scope '', but it is not defined
- Specification Pattern with Expressions causing error - variable 'x' of type '' referenced from scope '', but it is not defined
- EntityFrameworkCore Linq error: variable 'country.Country' of type 'Country' referenced from scope '', but it is not defined
- Error with an EF query with nested predicates - variable of type X referenced from scope but it is not defined
- LINQ expressions. Variable 'p' of type referenced from scope, but it is not defined
- MemberExpression: InvalidOperationExpression variable 'x' referenced from scope '', but it is not defined
- InvalidOperationException: type 'System.String' referenced from scope '', but it is not defined
- CRM 2011: An unhandled exception of type 'System.InvalidOperationException' occurred. referenced from scope '', but it is not defined
- Error when combining Lambda expressions: "variable 'foo' of type 'Foo' referenced from the scope '', but it is not defined
- Restrictions.On(_func) - Variable 'p' of type 'Product' referenced from scope'', but it is not defined on passthrough
- Expressions: Variable referenced from scope, but it is not defined exception
- Linq Expression referenced from scope but its not defined
- variable is referenced from scope, but not defined LINQ expression tree
- C# LINQ: 'variable 'x' of type referenced from scope '', but it is not defined'
- Any way to access to a variable of type "var", defined in code behind, from aspx file?
- The variable SI of type Microsoft.Xrm.Sdk.Entity is refenced on this scope, but it is not defined
- Ormlite + PredicateBuilder "variable referenced from scope '', but it is not defined"
- The type 'System.Data.Linq.DataContext' is defined in an assembly that is not referenced
- Accessing transparent identifier in select throws it is not referenced from scope
- An exception of type 'System.ArgumentException' occurred in EntityFramework.dll but was not handled in user code
- Aggregate Linq Expressions: getting an exception of variable referenced from scope
- Compile ERROR: The type is defined in an assembly that is not referenced
- How to propagate an anonymous type variable from using scope to outside?
- Error: An exception of type 'System.ArgumentException' occurred in System.Data.dll but was not handled in user code
- The specified cast from a materialized 'System.Int32' type to the 'System.Double' type is not valid
- Could not find an implementation of the query pattern for source type 'System.Data.Entity.DbSet'
- Nullable reference type information not exposed from FirstOrDefault
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
More Query from same tag
- Get next element from Query after element with specified Id. The LINQ expression could not be translated
- LINQ: How Can I Use Equals in query
- How to return "Grouped by" aggregated result?
- How can i keep reference of a lambda expression that will be later passed to OrderBy or LoadWith functions?
- ordering of OrderBy, Where, Select in the Linq query
- How do I build an expression to call an equality operator from a base class?
- Is it expensive to parse an ExpressionTree?
- foreach on IEnumerable or IList
- How to multiply two sequences in C# using lambda expressions?
- get distinct count and name using linq from dataset table
- I want to bring data from multiple tables with entityframework using linq method in dotnetcore web api controller
- Anonymous type with counter for each result
- Group neighboring values using linq
- Object field Contains arrayElements
- Can the IEnumerable.Zip resultsSelector add more than one element to the result Enumerable?
- How to fetch the whole last row for the end of each month
- How to query in .NET EF Core with Linq?
- linq to json target specific items to deserialize
- How to efficiently rewrite LINQ expression 'DbSet.Where(...Any(...))' that could not be translated in EFCore 3.1?
- C# : How to sort a list of object based on a list of string
- Query using LINQ to SharePoint with lambda expression against PeoplePicker - client object model
- Order values in a Gridview in ascending order with Linq
- Use LINQ to get items in one List<>, that are not in another List<>
- How to iterate over results of LINQ where query results using list<> in C#
- async await with Select() in Linq
- LinQ query - Cannot use Date as its non-primitive EDM type. Also cannot convert DateTime to String
- Count each columns in linq with left join c#
- IQueryable<out T> Where only class name is known
- How to Skip items efficiently from an IEnumerable which occasionally makes very slow requests?
- EF Model Query SELECT WHERE NOT IN