question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Change `Expression.Property` to `Expression.PropertyOrField`. ERROR: Instance property '<FIELD>' is not defined for type '<CLASS>'

See original GitHub issue

I’m using LINQ TO SQL IEnumerable

I’m getting the following exception. Instance property 'AssignedTo' is not defined for type 'IssueSearch' It was only starting to get thrown after adding this custom IssueSearch class with the embedded UserInfo.

Here’s my class

public class IssueSearch
{
    //<My Other Variables>
    public UserInfo AssignedTo; // <-- The Important One
}

public class UserInfo
{
    public string Id; // <-- The Important One
    //<My Other Variables>
}

Here’s my Filter<IssueSearch>.By. (I’ve included the Type’s, for reference)

Filter.By(
    "AssignedTo.Id", // string
    Operation.In, // ExpressionBuilder.Common.Operation
    [ "Id1", "Id2" ], //  object {System.Collections.Generic.List<object>}
    null, // object
    FilterStatementConnector.And); // ExpressionBuilder.Common.FilterStatementConnector

This is how I populate the IEnumerable<IssueSearch> using LINQ TO SQL

var query =
    (
        from i in d.Issues
        from aT in d.Users
            .Where(x => i.AssignedTo == x.Id)
            .DefaultIfEmpty()
         select new IssueSearch
         {
            //<My Other Variables>
            AssignedTo = new UserInfo
            {
                Id = aT.Id
                //<My Other Variables>
             }
        }
    )
    .Where(``Filter``); // <-- Here I Drop The Filter And The Exeption Is Thrown

Stack Trace: at System.Linq.Expressions.Expression.Property(Expression expression, String propertyName) at ExpressionBuilder.Helpers.BuilderHelper.GetMemberExpression(Expression param, String propertyName) at ExpressionBuilder.Builders.FilterBuilder.GetExpression(ParameterExpression param, IFilterStatement statement, String propertyName) at ExpressionBuilder.Builders.FilterBuilder.GetExpression[T](IFilter filter) at ExpressionBuilder.Generics.Filter`1.op_Implicit(Filter`1 filter)

Just a bit of background (FYI): I’ve built a master page that I can override Field values, pass Classes too, etc. etc… This way I can use the same master page, make some small changes to the child page, then I have a new page that has search functionality for a set DB table.

Also, sorry if this isn’t the right way of querying the issue

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:12 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
dbelmontcommented, Nov 15, 2017

Hi Glenn,

Many thanks for taking the time to log this issue. Feedback is always welcome! 😃

Now, regarding the issue, it’s a bit strange because (from what you’ve described) it should be working. I’m pretty sure we got unit tests for this specific case.

I’m about to release a new version within the next days and it’ll fix a bug around the usage of the library with LINQ To SQL. It doesn’t seem to be exactly your case, but I’d be really glad if you could check if this issue still happens in the new version. In this meanwhile, I’ll be investigating it.

Many thanks, David

0reactions
glenn2223commented, Dec 13, 2017

OMFG <- Pardon my French

After some more digging, I found the problem. I’m not going to tell you straight away though. First, I shall explain.

I saw the problem was with System.Linq.Expressions.Expression.Property(Expression expression, String propertyName). So I pulled up the method on Microsoft’s Dev Network, here.

I then placed my class, field etc. straight into that function… threw the same exception. Hmmm…

I thought, just for giggles, lets place their code in my solution. Just to make sure. Success! So, Microsoft hasn’t screwed me over. Back to, hmmm…

I then saw something, the method is called Expression.Property and their class consists of;

public int sample {get; set;}

Looked back at mine… WAIT A SECOND… OMFG. Mine is a field!!! Changed my testing method to Expression.Field and Success (well, partial success anyway).

Will you be adding support for Expression.Field? You can change it to Expression.PropertyOrField[LINK], or you can see if you can detect it (although this seems pointless to me)


I now have a new exception, grrr…

Message:

Expression of type 'System.Object[]' cannot be used for parameter of type 'System.String' of method 'Boolean Contains(System.String)'

Stack Trace:

at System.Linq.Expressions.Expression.ValidateOneArgument(MethodBase method, ExpressionType nodeKind, Expression arg, ParameterInfo pi)
at System.Linq.Expressions.Expression.ValidateArgumentTypes(MethodBase method, ExpressionType nodeKind, ReadOnlyCollection`1& arguments)

I can see the problem, however I use:

public static object ChangeType(object value, Type conversionType);

and (as you can see) this returns an object. Any help??

Read more comments on GitHub >

github_iconTop Results From Across the Web

Expression.Property(param, field) is "trolling" me [System. ...
The problem is that Expression.Property method does not support nested properties. It does exactly what it says - creates expression that ...
Read more >
Expression.PropertyOrField(Expression, String) Method
This method searches expression .Type and its base types for an instance property or field that has the name propertyOrFieldName . Static properties...
Read more >
Expression Class (System.Linq.Expressions)
Initializes a new instance of the Expression class. Properties. CanReduce. Indicates that the node can be reduced to a simpler node. If this ......
Read more >
Simplest Thing Possible: Dynamic Lambda Expressions
If the property name passed as a string isn't found for the specified type, an error is thrown. Next, you need to bring...
Read more >
Get Property Names Using Lambda Expressions in C# - ...
Explains how to pass properties as methods' parameters via lambda expressions. Build utility to get the properties names from the lambda expressions.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found