C# 我可以为IEnumerable向LINQ查询动态添加参数吗?有方法吗?
我创建了一个方法,它接受一个动态列表、一个对象和两个参数名。我想使用Enumerable.Any返回一个布尔值,该方法具有基于传递给该方法的参数名的匹配条件C# 我可以为IEnumerable向LINQ查询动态添加参数吗?有方法吗?,c#,linq,C#,Linq,我创建了一个方法,它接受一个动态列表、一个对象和两个参数名。我想使用Enumerable.Any返回一个布尔值,该方法具有基于传递给该方法的参数名的匹配条件 public static bool CheckDuplicate(List<T> list, Object obj, string param1, string param2) { return list.Any(item => item.pararm1 = obj.param1 && item.
public static bool CheckDuplicate(List<T> list, Object obj, string param1, string param2)
{
return list.Any(item => item.pararm1 = obj.param1 && item.param2 = obj.param2);
}
publicstaticboolcheckduplicate(列表列表,对象对象,字符串param1,字符串param2)
{
返回列表.Any(item=>item.pararm1=obj.param1&&item.param2=obj.param2);
}
我想根据动态提供的条件找到与obj对象的值相匹配的项。考虑创建一个类似LINQ的扩展方法
wherell
,它对作为参数给定的所有谓词执行Where
:
static IEnumerable<TSource> WhereAll<TSource>(this IEnumerable<TSource> source
IEnumerable<Func<TSource, bool>> predicates)
{
// TODO: exception if source / predicates null
// return all source elements that have a true for all predicates:
foreach (var sourceElement in source)
{
// check if this sourceElement returns a true for all Predicates:
if (predicates.All(predicate => predicate(sourceElement))
{
// yes, every predicate returns a true
yield return sourceElement;
}
// else: no there are some predicates that return false for this sourceElement
// skip this element
}
静态IEnumerable wherell(此IEnumerable源代码
IEnumerable谓词)
{
//TODO:源/谓词为空时异常
//返回所有谓词为true的所有源元素:
foreach(源中的var sourceElement)
{
//检查此sourceElement是否为所有谓词返回true:
if(谓词.All(谓词=>谓词(sourceElement))
{
//是的,每个谓词都返回一个true
收益要素;
}
//else:否此sourceElement有一些谓词返回false
//跳过此元素
}
用法:
List<Person> persons = ...
// Get all Parisians with a Name that were born before the year 2000:
var result = persons.WhereAll(new Func<Person, bool>[]
{
person => person.Name != null,
person => person.BirthDay.Year < 2000,
person => person.Address.City == "Paris",
});
列出人员=。。。
//让所有2000年以前出生的巴黎人都有一个名字:
var结果=人员。wherell(新函数[]
{
person=>person.Name!=null,
person=>person.birth.Year<2000,
person=>person.Address.City==“巴黎”,
});
似乎您想要的是比较通过成员变量名称访问的成员变量。这称为反射。以下是我的解决方案:
首先添加一个扩展方法,以帮助我们按名称获取成员变量(从):
那么,您的功能将是:
public static bool CheckDuplicate<T>(IEnumerable<T> list, object obj, string param1, string param2)
{
return list.Any(item =>
item.GetPropValue(param1).Equals(obj.GetPropValue(param1)) &&
item.GetPropValue(param2).Equals(obj.GetPropValue(param2))
);
}
在生产中使用时,您可能需要确保<代码> PARAM1和<代码> PARAM2实际上存在,并且也请查找并考虑<代码> .Quals](< /代码>和<代码>= >之间的区别。被装箱。
您可以传递一个Func
,而不是传递一个对象和字符串。但是在这一点上,您实际上只是定义了Any()
方法,而这个方法变得毫无意义。为什么不首先调用Any()
,而不是创建这个传递方法呢?
public static bool CheckDuplicate<T>(IEnumerable<T> list, object obj, string param1, string param2)
{
return list.Any(item =>
item.GetPropValue(param1).Equals(obj.GetPropValue(param1)) &&
item.GetPropValue(param2).Equals(obj.GetPropValue(param2))
);
}
static void Main(string[] args)
{
var theList = Enumerable.Range(0, 10).Select(i => new Tuple<int, int>(i, i + 1));
Console.WriteLine(CheckDuplicate(theList, new { Item1 = 5, Item2 = 6 }, "Item1", "Item2"));
Console.ReadKey();
}