score:8
actually i don't think the problem has anything to do with nullable types, but rather with value types. try your method with a property of type decimal
(not nullable<decimal>
) : it will fail the same way.
have a look at how expression trees are generated for value and reference types (using linqpad for instance)
expression<func<t, object>> lambda = x => x.astring;
(reference type)
=> the body is amemberexpression
expression<func<t, object>> lambda = x => x.adecimal;
(value type)
=> the body is aunaryexpression
withnodetype = convert
andtype = typeof(object)
, and itsoperand
is amemberexpression
i modified your method slightly to take that into account, and it seems to work fine :
parameterexpression param = expression.parameter(typeof(t), "arg");
expression member = expression.property(param, propertyname);
if (member.type.isvaluetype)
{
member = expression.convert(member, typeof(object));
}
return expression.lambda<func<t, object>>(member, param);
Source: stackoverflow.com
Related Query
- get property lambda from property Name (Where property type can be nullable)
- Retrieving Property name from lambda expression
- How to get property name from expression
- Get values by property name from an object at different levels
- Can I check a property on a generic object by passing in a lambda expression to get the parameter?
- How do I get property names of a type using a lambda expression and anonymous type?
- How can I get data from a list with a where clause to another list?
- Get class name and property name from an expression () => MyClass.Name
- How to get one property from Ilist<T> where other properties are True
- c# Lambda Expression - Get property value from string
- Dynamic lambda expression (OrderBy) and nullable property type
- How can I get distinct objects extracted from a property of a list of objects?
- How to get only objects from list where property x is not null in C#
- LINQ: Can I get a property from an object in a list, within a collection?
- Can someone tell me why this simple bit of c# code won't work, involves recursively calling method from within itself to get the root category ID
- How can I remove the time part from a nullable datetime in where condition of Linq
- how can i return model property from lambda expression (like mvc's "html.textboxfor(yyy)")?
- LINQ - get results where a certain property is in this other result set of the property's type
- Linq query to get the properties from object B which have the same name and type of object A
- Get lambda expression from where clause or IQueryable/IEnumerable
- get selected value from combobox with data source is anonymous type
- How can I query by property type rather than type name using LINQ?
- How can you use a LINQ Lambda Where Property Count = Something
- Get the Matched Property Name in linq where with multiple condition
- How can I get data from the Entity Framework if I only know the table name and the column name from which to get the data?
- How to get property from a nullable type?
- Get Table type when you have different table name from datacontext
- How to get a single value from a Dictionary<Key,Value> given a property that belongs to the type Value
- Linq query get sum from list where certain property equals to
- Can I get a property from a collection using Linq in a more elegant way than this?
More Query from same tag
- C#-Linq: Ignore case when comparing using string.equals in linq
- Pulling out XML values from a string in a database column
- C# Remove object from list stored in an ASP session
- How to get the latest date inserted from the table with Linq to entities
- LInq querying collection inside collection
- Entity Framework InvalidOperationException
- .Select is not supported in Microsoft.Azure.Documents.Linq
- LINQ to XML in c# . Enumeration yielded no results?
- Find a particular item in an object tree using LINQ
- Why does using Random in Sort causing [Unable to sort IComparer.Compare error]
- C# DataTable Filter Fastest Way
- Is there a String.IndexOf that takes a predicate?
- C# Paradigms: Side effects on Lists
- DataTable select distinct values from several columns
- Rewrite into a SortedList
- 405 (Method Not Allowed) in Angular JS With WCF Service
- How to aggregate data from inner objects in MongoDb?
- How to get an element within an attribute using LINQ
- EF Inserting data but all GUID turn to same with each other
- Why StringComparison.InvariantCultureIgnoreCase throws exception in where clause
- Difference between datatable.Rows.Cast<DataRow> and datatable.AsEnumerable() in Linq C#
- How to write LINQ query for fetching the specific records and generating new result at the same time?
- DataTableExtensions.AsEnumerable() throws InvalidCastException
- How to convert Dictionary <string,List<string>> in C# to csv with keys as headers?
- Does LINQ natively support splitting a collection in two?
- Lambda expression - Select into is not giving the results of all tables specified in the joins
- LINQ to SQL - Left OUTER Join with two joins not working
- LINQ: How to do JOIN using the linq extension method style on multiple fields?
- select two properties in an array of obj
- At what point is a LINQ data source determined?