score:0
public static expression<func<t, tpropertytype>> propertyselector<t, tpropertytype>(this ienumerable<t> collection, string propertyname)
{
if (string.isnullorwhitespace(propertyname))
{
throw new argumentexception(nameof(propertyname));
}
var properties = typeof(t).getproperties();
if (!properties.any(p => p.name == propertyname))
{
throw new objectnotfoundexception($"property: {propertyname} not found for type [{typeof(t).name}]");
}
var propertyinfo = properties.single(p => p.name == propertyname);
var alias = expression.parameter(typeof(t), "_");
var property = expression.property(alias, propertyinfo);
var functype = typeof(func<,>).makegenerictype(typeof(t), typeof(tpropertytype));
var lambda = expression.lambda(functype, property, alias);
return (expression<func<t, tpropertytype>>)lambda;
}
score:1
var propertyinfo = properties.single(p => p.name == propertyname);
var functype = typeof(func<,>).makegenerictype(typeof(t), propertyinfo.propertytype);
score:1
var expression = people.propertyselector<person, string>("lastname");
Source: stackoverflow.com
Related Query
- LambdaExpression to Expression via Extensions Method
- LINQ to Entities does not recognize the method 'Boolean HasFlag(System.Enum)' when creating the expression via System.Linq.Expressions.Expression
- LINQ Is it possible to get a method name without a return type via LINQ expression trees?
- LINQ to Entities does not recognize the method 'System.String ToString()' method, and this method cannot be translated into a store expression
- C# Pass Lambda Expression as Method Parameter
- LINQ: Passing lambda expression as parameter to be executed and returned by method
- Expression references a method that does not belong to the mocked object
- How to retrieve last 5 records using LINQ method or query expression in C#
- Difference between Query Expression and Method Expression in LINQ?
- LINQ to Entities does not recognize the method 'Int32 Int32(System.String)' method, and this method cannot be translated into a store expression
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.Object)' method, and this method cannot be translated into a store expression
- Expression of type 'System.Int32' cannot be used for parameter of type 'System.Object' of method 'Boolean Equals(System.Object)'
- LINQ to Entities does not recognize the method 'Int32 Parse(System.String)' method, and this method cannot be translated into a store expression
- Extension method returning lambda expression through compare
- Identify an event via a Linq Expression tree
- Extract method name from expression tree?
- Method GetPrice() cannot be translated into a store expression
- Differences in LINQ vs Method expression
- How do I build Expression Call for Any Method with generic parameter
- How to implement method with expression parameter c#
- extracting method name from linq expression
- Method cannot be translated into a store expression syntax error
- Syntax to refer a method returning an Expression to another method?
- Convert Method to Linq Expression for query
- Build expression tree for LINQ using List<T>.Contains method
- Call Ignore Case for Contains Method using a generic LINQ Expression
- Return Expression from a method to be used in OrderBy
- Best way to call an instance method in Expression Trees?
- LINQ to Entities does not recognize the method 'Int32 ToInt32(System.String)' method, and this method cannot be translated into a store expression
- Selecting a class method name using Lambda Expression
More Query from same tag
- optimize linq query with related entities
- Mongo Driver Linq Query throws Exception
- How to find if time is within A set of range?
- linq lambda expression and '&' operator
- Get a list of objects from DataGridView?
- Why does "SingleOrDefault" return null on second execution when there is data in DB?
- Inner queries in LINQ
- Sql linq query with where and group by
- Linq to Entities - SQL “IN” clause for integer column
- Lambda expression where column is equal to list items
- How do I display product counts grouping by product type and month?
- how to write action in which partial view action to be called
- How can i fix Cannot implicitly convert Generic.List< > to Generic.List< >
- How can I .Include down more than one level in LINQ?
- Linq query to compare 2 List<string> for distinct matches
- How to do a 'composite' select using LINQ?
- c# Turning a linq database record into a class instance
- How to use Linq Aggregate Query to print line sequence number alongwith some data from the List<string>?
- Dynamics 365 OrganizationServiceContext Linq Query with multiple Joins
- Can I create an xml file at a specified location using XElement?
- How do I copy projected results into another variable in c#?
- How to access grouped values returned by a linq query
- Using LINQ to get all results where ID in dictionary
- Using Linq to concatenate a list of property of classes
- Group by for list in list
- Can I use get/set method inside a incoming new object generated by Linq?
- Linq to Entities with custom method in Where
- Building a Complex Search Filter based on Linq
- Linq filtering a list by a composite key (contains and not contains)
- Error getting when trying assign linq data into DTO class