Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You can use a combination of reflection and dynamic expressions for this. I've put together a sample that might look a bit long at the first look. However, it matches your requirements and addresses these by </p> <ul> <li>Using reflection to find the properties that are of type string and match the property name - if provided.</li> <li>Creating an expression that calls string.Contains on all properties that have been identified. If several properties have been identified, the calls to string.Contains are combined by Or-expressions. This filter expression is compiled and handed to the Where extension method as a parameter. The provided list is filtered using the expression. </li> </ul> <p>Follow this <a href="http://ideone.com/XE7rm7" rel="nofollow">link</a> to run the sample.</p> <pre><code>using System; using System.Collections.Generic; using System.Reflection; using System.Linq; using System.Linq.Expressions; public class Test { public static IEnumerable&lt;T&gt; SelectItems&lt;T&gt;(IEnumerable&lt;T&gt; items, string propName, string value) { IEnumerable&lt;PropertyInfo&gt; props; if (!string.IsNullOrEmpty(propName)) props = new PropertyInfo[] { typeof(T).GetProperty(propName) }; else props = typeof(T).GetProperties(); props = props.Where(x =&gt; x != null &amp;&amp; x.PropertyType == typeof(string)); Expression lastExpr = null; ParameterExpression paramExpr = Expression.Parameter(typeof(T), "x"); ConstantExpression valueExpr = Expression.Constant(value); foreach(var prop in props) { var propExpr = GetPropertyExpression(prop, paramExpr, valueExpr); if (lastExpr == null) lastExpr = propExpr; else lastExpr = Expression.MakeBinary(ExpressionType.Or, lastExpr, propExpr); } if (lastExpr == null) return new T[] {}; var filterExpr = Expression.Lambda(lastExpr, paramExpr); return items.Where&lt;T&gt;((Func&lt;T, bool&gt;) filterExpr.Compile()); } private static Expression GetPropertyExpression(PropertyInfo prop, ParameterExpression paramExpr, ConstantExpression valueExpr) { var memberAcc = Expression.MakeMemberAccess(paramExpr, prop); var containsMember = typeof(string).GetMethod("Contains"); return Expression.Call(memberAcc, containsMember, valueExpr); } class TestClass { public string SomeProp { get; set; } public string SomeOtherProp { get; set; } } public static void Main() { var data = new TestClass[] { new TestClass() { SomeProp = "AAA", SomeOtherProp = "BBB" }, new TestClass() { SomeProp = "BBB", SomeOtherProp = "CCC" }, new TestClass() { SomeProp = "CCC", SomeOtherProp = "AAA" }, }; var result = SelectItems(data, "", "A"); foreach(var item in result) Console.WriteLine(item.SomeProp); } } </code></pre> <p>In contrast to a completely reflection based approach, this one assembles the filter expression only once and compiles it, so that I'd expect a (small) performance improvement.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload