110,561
社区成员
发帖
与我相关
我的任务
分享
String split = "张李王";
List<Func<Char, Boolean>> wheres = new List<Func<char, bool>>();
foreach (char item in split)
{
Func<Char, Boolean> func = (t) => t == item;
wheres.Add(func);
}
var list = new List<Char>();
foreach (var where in wheres)
{
list = list.Union(split.Where(t => where(t))).ToList();
}
Console.WriteLine(list.Count);
Console.Read();
class simple
{
public int number;
}
static List<simple> list = new List<simple>
{
new simple {number=1},
new simple {number=2},
new simple {number=3},
new simple {number=4},
new simple {number=5}
};
static void Main(string[] args)
{
var do1 = EvalEx.GetEval<Func<simple, bool>>(typeof(simple), "number!=1 && number!=2");
Console.Write(string.Join(",",(from d in list
where do1.Invoke(d)
select d.number.ToString()).ToList()));
Console.Read();
}
static class EvalEx
{
public static T GetEval<T>(Type type, string propertyPath)
{
if (type == null)
return default(T);
var func = System.Linq.Dynamic.DynamicExpression.ParseLambda(type, null, propertyPath);
var objParameter = System.Linq.Expressions.Expression.Parameter(typeof(object), "obj");
var objConvert = System.Linq.Expressions.Expression.Convert(objParameter, type);
var objInvoke = System.Linq.Expressions.Expression.Invoke(func, objConvert);
var resultExpression = System.Linq.Expressions.Expression.Lambda<T>(objInvoke, objParameter);
return resultExpression.Compile();
}
}