score:3
Accepted answer
i think you want something like:
public static expression<func<tresult, bool>> getpredicate<tkey>
(expression<func<tresult, tkey>> selector, tresult input, object value)
{
// note: move "early out" here so that bulk of method is less deeply nested.
// really? why make this generic in tkey then?
if (typeof(tkey) != typeof(string))
{
throw new exception("type not supported");
}
var parameter = expression.parameter("input");
var invocation = expression.invoke(selector, input);
var constant = expression.constant(value);
var equality = expression.equal(invocation, constant);
return expression.lambda<func<tresult, bool>>(equality, parameter);
}
i'm not quite sure what kind of equality that will use, mind you - it's entirely possible it's going to use a reference equality operation, whereas i suspect you want value equality. you'll have to try it to see, being careful in tests due to string interning.
score:0
return p => (selector.compile().invoke(input) as string) == value
Source: stackoverflow.com
Related Query
- How to understand the following C# linq code of implementing the algorithm to return all combinations of k elements from n
- Passing Func to Where changes return type from IQueryable to IEnumerable
- creating Linq to sqlite dbml from DbLinq source code
- Return Func from Func
- c# Linq or code to extract groups from a single list of source data
- EF Code First- Return Records from Many to Many Table
- LINQ query to return distinct field values from list of objects
- How to return value from Action()?
- How can I filter a dictionary using LINQ and return it to a dictionary from the same type
- How to return anonymous type from c# method that uses LINQ to SQL
- Return Anonymous Type from a function
- Which LINQ statements force Entity Framework to return from the DB?
- C# 3.0: Need to return duplicates from a List<>
- Linq to return ALL pairs of elements from two lists?
- This code returns distinct values. However, what I want is to return a strongly typed collection as opposed to an anonymous type
- Expression<Func<T, bool>> from a F# func
- How can I return an anonymous type from a method?
- Linq to Objects - return pairs of numbers from list of numbers
- How to return a page of results from SQL?
- Enumerable.Empty<T>().AsQueryable(); This method supports the LINQ to Entities infrastructure and is not intended to be used directly from your code
- Accessing Results View from variable in code
- Generating the Shortest Regex Dynamically from a source List of Strings
- LINQ to Entities - How to return a single string value from entity
- Is there a better way to return the next item in a list and loop from the end to the front?
- Return best fit item from collection in C# 3.5 in just a line or two
- How keep carriage return from parsing XML
- How to pass LinQ Expressions from F# to C# code
- Return one result from LINQ Query
- How to return a list from a method in C#
- Return Type for Collection from LINQ Query
More Query from same tag
- linq query fails only on Contains "Object reference not set to an instance of an object"
- LINQ operation on an IQueryable that joins to another IQueryable that needs to be group byed?
- Count startswith with linq
- Summarising existing data into newly created record
- How to make pyramid shape asp.net datalist?
- Linq to Xml : Exception -The ' ' character, hexadecimal value 0x20, cannot be included in a name
- Object does not contain "fullname" LINQ error
- Getting key-value combinations from a Dictionary using LINQ
- How to remove element from XML
- Join 2 table and group 2 field in linq
- changing item position in a list
- Explicit construction of entity type '' in query is not allowed
- how to filter and sum using linq
- Difference between contains and contains<> in c#
- is there something faster than Enumerable.Except<TSource> Method?
- How could I accomplish this in Linq?
- Selecting an enemy from a non sorted weighted list
- Search in the List Item Using C#
- Extract key and value from a List of a List of objects using LINQ
- sub sub child node attributes according to some attribute value of child node of xml using linq
- how to group by over anonymous type with vb.net linq to object
- use for loop to iterate through Linq group result set
- Error when searching for string containing &
- ASP.NET MVC Recursion Procedure
- Efficiently select random rows from large resultset with LINQ (ala TABLESAMPLE)
- Group list by element number
- Dictionary of dictionaries - IEnumerable implementation in C#
- Is it possible that filter IIS worker processes with more criteria?
- LINQ add sum of columns to list entry
- Create nodes based on id values of other nodes