score:4
that's because inside your expression you are accessing a field. the exception tells you that you are accessing a field.
the expression is not evaluated when you create the query. it is only executed once you execute it. at that point, it will need to resolve the field. a workaround is getting the expression first into a local variable:
private static string getsomething(iqueryable<enumtest> things)
{
var expression = program.converttostring;
var ret = things
.asexpandable()
.select(c => expression.invoke(c.someenum))
.first();
return ret;
}
seeing that you are using this with entityframework, what will happen is that your expression will be converted to a sql query. however, since you are accessing a class inside the expression, it cannot convert this to a sql statement (how would it do that?). when you have an instance of the expression (with the local variable) you are eliminating this class access and that expression can be converted into sql.
Source: stackoverflow.com
Related Query
- Why am I getting an InvalidCastException when using an expression from a static field?
- Getting InvalidCastException when trying to implement sorting in Entity Framework Code First using Linq
- How to reuse a linq expression for 'Where' when using multiple source tables
- Why do I get a "key already added" error when using the key from a GroupBy in ToDictionary?
- Access property in lambda expression from string when using LINQ
- Getting the first result from a LINQ query - why does ElementAt<T>(0) fails when First<T>() succeeds?
- Getting weird behavior when retrieving data from Microsoft CRM using LINQ
- Why did my code work when I changed it from IEnumerable to List?
- Why is generated query from expression using case instead of where statements?
- Getting error when fetching data from Sql server compact using linq
- Getting null reference error in linq expression when trying to join more than 2 tables using defaultifempty method
- Using Linq when getting value from nested list
- Why does my method not run when I attempt to delete a Record from my database using a strongly-typed Delete View?
- Why am I getting this Linq to Nhibernate exception when I add the same expression to the query twice?
- Updating List using LINQ working when execute from Immediate window, not from code direct
- Entity-framework code is slow when using Include() many times
- Why does this Linq Cast Fail when using ToList?
- Why does C# compiler create private DisplayClass when using LINQ method Any() and how can I avoid it?
- Why can't you edit and continue debugging when there's a Lambda expression in the method?
- Why am I getting "Collection was modified; enumeration operation may not execute" when not modifying the enumerated collection?
- How to remove an element from an xml using Xdocument when we have multiple elements with same name but different attributes
- Why do I have to copy "this" when using LINQ in a struct (and is it OK if I do)?
- Get N max numbers from a List<int> using lambda expression
- Why throws exception when using Guid.Parse() method?
- Why does this error occur when using SingleAsync?
- When using "yield" why does compiler-generated type implement both IEnumerable and IEnumerator
- Why is this LINQ query not executed when using foreach?
- Using LINQ expression to retrieve IEnumerable of properties from IEnumerable of POCO
- Why is IQueryable twice as fast than IEnumerable when using Linq To Objects
- Remove a condition from an expression using an ExpressionVisitor
More Query from same tag
- How can I convert HQL inner join to LINQ join
- Deleting an item with entity framework
- Invalid Argument - Interfaces inherited, as works, implicit casting does not. What gives?
- LINQ to SQL with a large database table increase performance
- Clean up data into a DataTable by Creating separate Groups using DataTable
- How do I sort on a column that isn't in the database with LINQ to entities?
- Find count of an list2.item in list1
- How to convert a var which contains Long to an Long[]
- Compare DateTime as a string into a LINQ to Entities doesn't recognize DateTime.parse(string)
- How to convert var to string[]
- How I Can use Linq Query count no. of record in query
- How to convert List to a string and back
- Add " | " in string if where string value is not null or empty
- Is there a LINQ way to turn 2 line items into N based on quantity?
- LINQ: multiple Where calls only uses last one?
- getting a list of values which contain objects which have an id field of x
- Select products that has all tags
- Nice, clean cross join in Linq using only extension methods
- Validating and Extracting XML record by record into Database
- Two CSV files to one output, join and add data on column(s)
- What do i use instead of e.Values.add(... for the ListViewEditEventArgs to upload an Image
- silent fail when updating a row via linq to sql
- LINQ - outer join to parent class in EF code first
- Linq Count method and Performance
- How do I return a default value if collection is empty?
- how to remove repeating elements in an array using LINQ?remove an element if its repeating?
- LINQ to SQL: Select count rows for each date in a provided range?
- Three-way join using one SQL table
- LINQ to Entities does not recognize the method 'System.String ToString()' method when converting Nullable DateTime
- C# Find best matching element / Simplify query to List