本文介绍了执行两个LINQ IQueryable的OR的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个IQueryable:
public static IQueryable< Counterparty> SearchByCode(这个IQueryable< Counterparty>可查询,字符串searchQuery)
{
return queryable.Where(x => x.Code.StartsWith(searchQuery.ToUpper()));
}
public static IQueryable< Counterparty> SearchByName(这个IQueryable< Counterparty>可查询,字符串searchQuery)
{
return queryable.Where(x => x.Name.ToLower()。包含(searchQuery.ToLower()))
}
我想创建另一个IQueryable,它将两个查询组合为一个OR,例如
public static IQueryable< Counterparty> SearchByCodeOrName(此IQueryable< Counterparty>可查询的字符串searchQuery)
/ pre>
{
//返回SearchByName(searchQuery)或SearchByCode(searchQuery)
}
我宁愿避免重复的代码,所以我想重用现有的可查询,而不是重写一个新的lambda表达式,如
Where(x => x.Code.StartsWith .... || x.Name.Contains .....)
解决方案根据你想要重用的代码,这可能是一个替代方法,在这里你不会重复查询条件的内部工作:
private static Func< Counterparty,string,bool> CodeStartsWith =(x,searchQuery)=> x.Code.StartsWith(searchQuery.ToUpper());
private static Func< Counterparty,string,bool> NameContains =(x,searchQuery)=> x.Name.Contains(searchQuery.ToLower());
public static IQueryable< Counterparty> SearchByCode(这个IQueryable< Counterparty>可查询,字符串searchQuery)
{
return queryable.Where(x => CodeStartsWith(x,searchQuery));
}
public static IQueryable< Counterparty> SearchByName(这个IQueryable< Counterparty>可查询的字符串searchQuery)
{
return queryable.Where(x => NameContains(x,searchQuery));
}
public static IQueryable< Counterparty> SearchByCodeOrName(这个IQueryable< Counterparty>可查询的字符串searchQuery)
{
return queryable.Where(x => NameContains(x,searchQuery)|| CodeStartsWith(x,searchQuery));
}
I've got two IQueryables:
public static IQueryable<Counterparty> SearchByCode(this IQueryable<Counterparty> queryable, string searchQuery) { return queryable.Where(x => x.Code.StartsWith(searchQuery.ToUpper())); } public static IQueryable<Counterparty> SearchByName(this IQueryable<Counterparty> queryable, string searchQuery) { return queryable.Where(x => x.Name.ToLower().Contains(searchQuery.ToLower())); }
I want to create another IQueryable which combines the two queries as an OR e.g
public static IQueryable<Counterparty> SearchByCodeOrName(this IQueryable<Counterparty> queryable, string searchQuery) { // return SearchByName(searchQuery) OR SearchByCode(searchQuery) }
I'd rather avoid duplication of code so I want to reuse the existing queryables rather than rewriting a new lambda expression like
Where(x=> x.Code.StartsWith.... || x.Name.Contains.....)
解决方案Depending on what code you do want to be able to reuse, this could be an alternative way of doing it where you are not duplicating the inner workings of your query conditions:
private static Func<Counterparty, string, bool> CodeStartsWith = (x, searchQuery) => x.Code.StartsWith(searchQuery.ToUpper()); private static Func<Counterparty, string, bool> NameContains = (x, searchQuery) => x.Name.Contains(searchQuery.ToLower()); public static IQueryable<Counterparty> SearchByCode(this IQueryable<Counterparty> queryable, string searchQuery) { return queryable.Where(x => CodeStartsWith(x, searchQuery)); } public static IQueryable<Counterparty> SearchByName(this IQueryable<Counterparty> queryable, string searchQuery) { return queryable.Where(x => NameContains(x, searchQuery)); } public static IQueryable<Counterparty> SearchByCodeOrName(this IQueryable<Counterparty> queryable, string searchQuery) { return queryable.Where(x => NameContains(x, searchQuery) || CodeStartsWith(x, searchQuery)); }
这篇关于执行两个LINQ IQueryable的OR的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!