3

The entity field is not recognized in the following Where clause. Is the VB wrong?

Dim projects = context.projects
               .OrderBy(Function(x) x.name)
               .Select(Function(x) {x.id, x.name})
               .Where(Function(x) x.id <> sourceid)

If I take the Where off, it works fine. Also, if I flip the Where and the OrderBy, Where is fine, but now OrderBy fails.

2
  • What is the exact error message? Commented Jul 22, 2014 at 13:38
  • @DanPuzey - Cannot resolve Symbol 'id' Commented Jul 22, 2014 at 13:39

3 Answers 3

3

Try this:

Dim projects = context.projects
                               .OrderBy(Function(x) x.name)
                               .Select(Function(x) New With {x.id, x.name})
                               .Where(Function(x) x.id <> sourceid)

The New With keyword should create an IEnumerable of anonymous type. You can then work with the id property in the Where clause without having to change the order of your operations.

There is nothing stopping you from doing the operations OrderBy, Select, Where in that order. The above code will certainly compile and run. However, logically you need to do the Where before Select, since the former is a filtering operation, while the latter is a projection.

Sign up to request clarification or add additional context in comments.

Comments

2

Can you please try with the below code snippet.

Dim projects = context.projects.Where(Function(x) x.id <> sourceid).OrderBy(Function(x) x.name).Select(Function(x) {x.id, x.name})

7 Comments

Where clause needs to be before the select clause.
@bowlturner - Is this only a VB.NET thing? I initially had it in C# and it worked fine.
Good question, I just know that it's "supposed" to be Where, OrderBy, Select, maybe you changed something you didn't realize when converting to VB? An Extra pair of parentheses could make a big difference.
@bowlturner No it doesn't - I suspect the problem is the type change that the Select performs.
Ah yes, VB's loose typing could be to blame
|
0

This {x.id, x.name} is most likely an array of object (assuming id is integer and name is string, VB would infer Object). It is not an instance of a class with properties of id and name. @shree.pat18 explained how it can be adjusted to return what you want, but I would suggest using query syntax for clarity, and also putting your where clause before Select (should be slightly faster, because it does not create anonymous objects from the values you don't need included in results):

Dim projects = From p In context.projects
               OrderBy p.name
               Where p.Id <> sourceid
               Select Id = p.Id, Name = p.Name

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.