简体   繁体   中英

Writing out 'AND' and 'OR' in LINQ

How do you write out 'AND' and 'OR' in LINQ. Ive tried the 'AND' keyword as well as '&&' and a ','.

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 || for OR
in VB , AND for AND and OR for OR

now, what language are you using?

UPDATE 1

you want to join the two tables first right?

UNION method excludes duplicates from the return set.

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:

    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. 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.

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.

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 .

What you want is Concat because I assume with scores you generally don't want to exclude anything, which Union will do.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM