[英]Writing out 'AND' and 'OR' in LINQ
How do you write out 'AND' and 'OR' in LINQ. 如何在LINQ中写出“ AND”和“ OR”。 Ive tried the 'AND' keyword as well as '&&' and a ','. Ive尝试了“ AND”关键字以及“ &&”和“,”。
I have also googled it and havent come across anything that helps. 我也用谷歌搜索,还没有遇到任何帮助。
Any help will be highly appreciated Thanks 任何帮助将不胜感激谢谢
EDIT: 编辑:
int[] moreScores = new int[]{12,12,45,45,87,96};
int[] scores = new int[] { 97, 92, 81, 60 };
// Define the query expression.
IEnumerable<int> scoreQuery =
from score in scores
and moreScores
where score > 80
select score;
it depends on the langauge you use 这取决于您使用的语言
in C# , &&
for AND and ||
在C#中 , &&
代表AND和||
for OR 或
in VB , AND
for AND and OR
for OR 在VB中 , AND
为AND和OR
为OR
now, what language are you using? 现在,您使用什么语言?
UPDATE 1 更新1
you want to join the two tables first right? 您想先加入两个表吗?
UNION
method excludes duplicates from the return set. UNION
方法从返回集中排除重复项。
IEnumerable<int> scoreQuery = from score in (scores.Union(moreScores))
where score > 80
select score;
If you give us correct example, you should use Union, not AND: 如果您提供正确的示例,则应使用Union,而不要使用AND:
IEnumerable<int> scoreQuery =
from score in scores.Union(moreScores)
where score > 80
select score;
You cannot simply query two different arrays by putting AND in between them. 您不能通过在两个不同的数组之间插入AND来简单地查询它们。 Try the following code: 尝试以下代码:
var moreScores = new int[] { 12, 12, 45, 45, 87, 96 };
var scores = new int[] { 97, 92, 81, 60 };
var scoreQueryResults =
from score in (scores.Union(moreScores))
where score > 80
select score;
Also some generic example for Linq. 还有一些有关Linq的通用示例。
var list = new List<string>();
// ... add some items
// Searching for the strings that starts with J AND ends K
// Method Chain Format
var results1 = list.Where(item => item.StartsWith("J") && item.EndsWith("K"));
// Query Format
var results2 = from item in list
where item.StartsWith("J") && item.EndsWith("K")
select item;
// Searching for the strings that starts with J OR K
// Method Chain Format
var results3 = list.Where(item => item.StartsWith("J") || item.StartsWith("K"));
// Query Format
var results4 = from item in list
where item.StartsWith("J") || item.StartsWith("K")
select item;
I think you are trying to write something like this... 我认为您正在尝试编写类似这样的内容...
int[] moreScores = new int[]{12,12,45,45,87,96};
int[] scores = new int[] { 97, 92, 81, 60 };
// Define the query expression.
IEnumerable<int> scoreQuery = from score in scores.Union(moreScores)
where score > 80
select score;
Linq doesen't work like that, you first have to combine your two sets. Linq不能那样工作,您首先必须将两个集合结合起来。
var allScores = scores.Concat(morescores);
Then you could do, 那你可以做
var scoreGreaterTham80 = allScores.Where(s => s > 80);
if you wanted to exclude duplicates between scores
and morescores
use Union
instead of Concat
. 如果您想排除scores
和morescores
scores
之间的重复项, morescores
使用Union
而不是Concat
。
What you want is Concat
because I assume with scores you generally don't want to exclude anything, which Union
will do. 您想要的是Concat
因为我认为您通常不希望排除分数 ,而Union
会这样做。
int[] moreScores = new int[] { 12, 12, 45, 45, 87, 96 };
int[] scores = new int[] { 97, 92, 81, 60 };
IEnumerable<int> scoreQuery = from score in moreScores.Concat(scores)
where score > 80
select score;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.