C# 如果其他在 LINQ
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/443044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
If Else in LINQ
提问by Graviton
Is it possible to use If Else conditional in a LINQ query?
是否可以在 LINQ 查询中使用 If Else 条件?
Something like
就像是
from p in db.products
if p.price>0
select new
{
Owner=from q in db.Users
select q.Name
}
else
select new
{
Owner = from r in db.ExternalUsers
select r.Name
}
采纳答案by Richard Ev
This might work...
这可能有效...
from p in db.products
select new
{
Owner = (p.price > 0 ?
from q in db.Users select q.Name :
from r in db.ExternalUsers select r.Name)
}
回答by Marc Gravell
I assume from db
that this is LINQ-to-SQL / Entity Framework / similar (not LINQ-to-Objects);
我假设db
这是 LINQ-to-SQL / Entity Framework / 类似(不是 LINQ-to-Objects);
Generally, you do better with the conditional syntax ( a ? b : c) - however, I don't know if it will work with your different queries like that (after all, how would your write the TSQL?).
通常,您使用条件语法 (a ? b : c) 做得更好 - 但是,我不知道它是否适用于您这样的不同查询(毕竟,您将如何编写 TSQL?)。
For a trivial example of the type of thing you cando:
对于您可以执行的操作类型的一个简单示例:
select new {p.PriceID, Type = p.Price > 0 ? "debit" : "credit" };
You can do much richer things, but I really doubt you can pick the tablein the conditional. You're welcome to try, of course...
您可以做更丰富的事情,但我真的怀疑您是否可以在条件中选择表格。当然,欢迎您尝试……
回答by Cheung
Answer above is not suitable for complicate Linq expression. All you need is:
上面的答案不适合复杂的 Linq 表达式。所有你需要的是:
// set up the "main query"
var test = from p in _db.test select _db.test;
// if str1 is not null, add a where-condition
if(str1 != null)
{
test = test.Where(p => p.test == str);
}
回答by Lucascio Phan
you should change like this:
你应该像这样改变:
private string getValue(float price)
{
if(price >0)
return "debit";
return "credit";
}
//Get value like this
select new {p.PriceID, Type = getValue(p.Price)};
回答by Bianca Kalman
my example:
我的例子:
companyNamesFirst = this.model.CustomerDisplayList.Where(a => a.CompanyFirst != null ? a.CompanyFirst.StartsWith(typedChars.ToLower())) : false).Select(b => b.CompanyFirst).Distinct().ToList();
回答by Tony Trembath-Drake
var result = _context.Employees
.Where(x => !x.IsDeleted)
.Where(x => x.ClientId > (clientId > 0 ? clientId - 1 : -1))
.Where(x => x.ClientId < (clientId > 0 ? clientId + 1 : 1000))
.Where(x => x.ContractorFlag == employeeFlag);
return result;
If clientId = 0 we want ALL employees,. but for any clientId between 1 and 999 we want only clients with that ID. I was having issues with seperate LINQ statements not being the same (Deleted/Clients filters need to be on all queries), so by add these two lines it works (all be it until we have 999+ clients - which would be a happy re-factor day!!
如果 clientId = 0 我们想要所有员工。但是对于 1 到 999 之间的任何 clientId,我们只需要具有该 ID 的客户端。我遇到了单独的 LINQ 语句不一样的问题(Deleted/Clients 过滤器需要在所有查询上),所以通过添加这两行它可以工作(直到我们有 999+ 个客户端 - 这将是一个快乐的重新-因素日!!