DeSon
DeSon

Reputation: 147

C# linq: Check multiple conditions on a list on the same property field

Am trying to get 'testNumber' when 'name' matches with 'AAA' & 'BBB' like below, but '_check' bool field is false'. What am I missing?

 static void Main(string[] args)
    {
        var _tests = new List<testDetails>()
        {
            new testDetails { testNumber = "1111111", name =  "AAA"  },
            new testDetails { testNumber = "1111111",  name =  "BBB"   },
            new testDetails { testNumber = "1111111",  name =  "CCC"   },
            new testDetails { testNumber = "1111111",  name =  "DDD"   },
            new testDetails { testNumber = "222222", name =  "AAA"  },
            new testDetails { testNumber = "22222",  name =  "BBB"   },
            new testDetails { testNumber = "33333",  name =  "CCC"   },
            new testDetails { testNumber = "11111",  name =  "EEE"   },
            new testDetails { testNumber = "33333",  name =  "CCC"   },
            new testDetails { testNumber = "55555",  name =  "DDD"   }
        };
    
        foreach (var _test in _tests.GroupBy(x => x.testNumber).ToList())
        {
            bool _check = _test.Where(o => o.name.Equals("AAA") && o.name.Equals("BBB")).Any();
           //_test.Any(p => p.name.Equals("AAA") && p.name.Equals("BBB")) - false
            //why is _check false?
            if (_check)
            {
                string _string = string.Empty;
                _string = _test.Where(o => o.name.Equals("AAA") && o.name.Equals("BBB")).Select(q => q.testNumber).FirstOrDefault();
            }
        }
    }

Upvotes: 0

Views: 3007

Answers (5)

Nicolas
Nicolas

Reputation: 46

Maybe what you are looking for is :

bool _check = _test.Any(o => o.name.Equals("AAA")) && _test.Any(o => o.name.Equals("BBB"));

This will check if your group contain at least one testDetails with "AAA" and one with "BBB"

Your current code cannot work because one instance of testDetails cannot have his name equal to both "AAA" and "BBB" at the same time

Upvotes: 3

Andrew Corrigan
Andrew Corrigan

Reputation: 1057

It looks like it's as simple as you've got confused between an AND and an OR - the query you've done is getting all values where name is "AAA" AND name is "BBB"; but if name is "AAA", name cannot be "BBB".

If you swap the && to an || then you'd be getting the values where name is either "AAA" or "BBB"

Upvotes: 0

Lewis Taylor
Lewis Taylor

Reputation: 728

bool _check = _test.Where(o => o.name.Equals("AAA") && o.name.Equals("BBB")).Any();

o.name cannot equal "AAA" and "BBB" at the same time. Should it be an or operation?

Upvotes: 1

Lin Yi-Sheng
Lin Yi-Sheng

Reputation: 71

o.name.Equals("AAA") && o.name.Equals("BBB")

This code always false, maybe what you want is this code.

o.name.Equals("AAA") || o.name.Equals("BBB")

Upvotes: 2

jason.kaisersmith
jason.kaisersmith

Reputation: 9610

You are currently checking if the same object has both the name "AAA" and the name "BBB".

Well the same object cannot have both. So you should be using an OR statement instead.

bool _check = _test.Where(o => o.name.Equals("AAA") || o.name.Equals("BBB")).Any();

Upvotes: 3

Related Questions