是否可以使用where子句编写linq select,可以选择ALL项目还是特定项目?在SQL中,我可以使用“像'%'这样的货币”来返回所有内容。 我正在尝试编写一种方法,以便我可以传递货币(在其他一些东西中)并重复使用相同的方法。
e.g。
Just GBP
from a in accounts
where currency.Contains('GBP')
select a
Just GBP
from a in accounts
where currency == 'GBP'
select a
所有货币?
from a in accounts
where currency like '%'
select a
答案 0 :(得分:7)
您是否尝试“存储”查询并在稍后的步骤中对其进行过滤,如下所示:
IEnumerable<AccountClass> GetAccounts(string filter = null)
{
var query = from a in accounts select a;
if (!string.IsNullOrEmpty(filter))
{
query = query.Where(a => a.Currency.Contains(filter));
}
return query;
}
这可以在单个查询中折叠,但对我来说似乎不太可读,可能无法使用LINQ-to-SQL(或其他LINQ-to- DB ,其中表达式被转换为查询):
from a in accounts
where string.IsNullOrEmpty(filter) || a.Currency.Contains(filter)
select a
答案 1 :(得分:2)
你可以尝试
.Where(c => currencyToFind == null ? true : c==currencyToFind)
如果您想要所有货币,请为您想要的货币传递空值。
在linq查询表达式中:
from a in accounts
where (currencyToFind == null ? true : account.Currency==currencyToFind)
select a
答案 2 :(得分:1)
如果您想要退回所有货币,请不要使用任何where
(在LINQ和SQL中):
from a in accounts
select a
或者,如果您真的不需要做任何其他事情,只需使用accounts
。
编辑:如果你想拥有一种方法,比如任何货币由null
代表,你可以这样做:
IQueryable<Account> result = accounts;
if (currency != null)
result = result.Where(a => a.Currency == currency);
return result;
答案 3 :(得分:0)
你不能只过滤条件吗?
var selectedAccounts = accounts.Select(y => y);
if(!string.IsNullOrEmpty(currency))
{
selectedAccounts = selectedAccounts.Where(y => y.Currency.Contains(currency));
}
你甚至可以尝试更通用的版本:
IEnumerable<Account> GetAccounts(Func<Account, bool> filter)
{
var selectedAccounts = accounts.Select(y => y);
if(filter != null)
{
selectedAccounts = selectedAccounts.Where(filter);
}
return selectedAccounts;
}
IEnumerable<Account> GetAccountsForCurrency(string currency)
{
if(string.IsNullOrEmpty(currency))
{
return GetAccounts(null);
}
return GetAccounts((y) => y.Currency.Contains(currency));
}
现在您还有一个可用于不同类型过滤的特定方法和一个更通用的方法。
答案 4 :(得分:0)
如果没有更多信息,很难回答这个问题,但这里有你想要的那种模式(除非得到保证,否则我的书中会过度设计):
public static Expression<Func<Account, bool>> GetAccountCurrencyPredicate
(this FilterKind filter, string value)
{
switch (filter)
{
case FilterKind.Exact:
return account => account.Currency == value;
case FilterKind.Contains:
return account => account.Currency.Contains(value);
case FilterKind.All:
return account => true;
default:
throw new ArgumentException("Unknown FilterKind.", "filter");
}
}
然后将其用作:
FilterKind filter = ...
string value = ...
IQueryable<Account> accounts = ...
var predicate = filter.GetAccountCurrencyPredicate(value);
var matchingAccounts = accounts.Where(predicate);
答案 5 :(得分:0)
我可以用两种方式解释你的问题(目前的情况):
<强> 1。如何制定选择所有内容的Where
条件?
简单:省略Where
过滤器运算符:
public void WithCurrency(this IEnumerable<Account> accounts, string currency)
{
return accounts.Where(account => account.Currency == currency);
}
public void WithAnyCurrency(this IEnumerable<Account> accounts)
{
return accounts; // no .Where(…) filter needed!
}
<强> 2。如何在LINQ查询中使用SQL的LIKE
模式匹配运算符?
如果这就是您要求的内容,我会在此向您推荐现有问题Like operator in Linq。
答案 6 :(得分:-1)
如果要返回任何内容,为什么使用where
语句?
where
用于过滤,因此如果您想要返回任何内容,可以尝试
from a in accounts
select a
或者,如果你想在其中有where语句
from a in accounts
where 1 = 1
select a
或者,如果你想要一个类似的陈述我建议使用正则表达式
from a in accounts
let r = new Regex("expression")
where r.IsMatch(a.currency)
select a;