Linq OR Operator?

linq tutorial
linq methods
linq query
language extensions in linq
linq to sql
linq syntax
in operator in linq lambda expression
linq select query with where clause in c#

I have Dogs and Cats classes derived from Animal. I want to find the first black colored animal. I'm currently doing it as follows:

Animal blackAnimal = dogs.FirstOrDefault(d => d.Color == "Black");
if (blackAnimal == null)
{
    blackAnimal = cats.FirstOrDefault(c => c.Color == "Black");
}

I want to be able to do it in the same statement - such that find the first black dog, but if there's no black colored dog, find the first black colored cat.

Notice that the Color property is not from Animal, but rather independent properties on Dog and Cat.

Diego Torres answer is perfect but I will provide another way to do it which can be applicable where his solution will not work:

Animal blackAnimal = (Animal)dogs.FirstOrDefault(d => d.Color == "Black") ?? cats.FirstOrDefault(c => c.Color == "Black");

The Standard LINQ Operators, LINQ - Query Operators - A set of extension methods forming a query pattern Operator, Description, C# Query Expression Syntax, VB Query Expression Syntax​. Standard Query Operators Overview (C#) 07/20/2015; 3 minutes to read +4; In this article. The standard query operators are the methods that form the LINQ pattern. Most of these methods operate on sequences, where a sequence is an object whose type implements the IEnumerable<T> interface or the IQueryable<T> interface.

With Concat:

dogs
    .Cast<Animal>()
    .Concat(cats)
    .FirstOrDefault(d => d.Color == "Black");

It will enumerate the second collection after the first.


Since you are searching on different properties then you can do

var result = dogs.FirstOrDefault(x => x.Color1 == "Black") as Animal ??
             cats.FirstOrDefault(x => x.Color2 == "Black");

LINQ - Query Operators, LINQ (Language Integrated Query) is uniform query syntax in C# and VB.NET to retrieve data from different sources and formats. It is integrated in C# or VB,  A set of extension methods forming a query pattern is known as LINQ Standard Query Operators. As building blocks of LINQ query expressions, these operators offer a range of query capabilities like filtering, sorting, projection, aggregation, etc. LINQ standard query operators can be categorized into the following ones on the basis of their

You will need to .Cast it all if the collections are not of the base type.

Given that Color is not a property of Animal, you will have to do something like this:

var animal = dogs
    // check for dogs
    .Where(d => d.Color == "Black").Cast<Animal>()
    // check for cats
    .Concat(cats.Where(c => c.Color == "Black").Cast<Animal>())
    // get the first
    .FirstOrDefault();

What is LINQ, This tutorial explains LINQ query syntax. Collection> <Standard Query Operators> <lambda expression> <select or groupBy operator> <result formation​>. What are LINQ Operators? The LINQ Operators are nothing but a set of extension methods that are used to write LINQ Query. These LINQ extension methods provide lots of very useful features which we can apply to the data source. Some of the features are filtering the data, sorting the data, grouping the data, etc.

LINQ Query Syntax, The LINQ Operators are nothing but a set of extension methods that are used to write LINQ Query. These LINQ extension methods provide lots of very useful  At compile time, query expressions are converted to Standard Query Operator method calls according to the rules set forth in the C# specification. Any query that can be expressed by using query syntax can also be expressed by using method syntax.

Language Integrated Query, When creating an anonymous type (what you're doing with the "new" without specifying a type) you have to specify the member name for each  Join Operators. In LINQ there is a Join operator and also a GroupJoin operator. The Join operator is similar to a SQL INNER JOIN in the sense that it only outputs results when it finds a match between two sequences and the result itself is still a flat sequence. You typically need to perform some projection into a new object to see information

LINQ Operators in C# Examples, The object comparison functions are useful for all objects, and are named after the rich comparison operators they support: operator. lt (a, b)¶; operator. le (  In a LINQ query, the first step is to specify the data source. In C# as in most programming languages a variable must be declared before it can be used. In a LINQ query, the from clause comes first in order to introduce the data source ( customers) and the range variable ( cust ). //queryAllCustomers is an IEnumerable<Customer> var

Comments
  • shouldn't the Color property be in Animal class?
  • Not allowing me to do this either - Operator '??' cannot be applied to operands of type 'Dog' and 'Cat'.
  • This should be dogs.FirstOrDefault(d => d.Color == "Black") as Animal ?? cats.FirstOrDefault(c => c.Color == "Black") as Animal;
  • @Gengis yes sorry, it has to be casted otherwise it will not be able to find a best match
  • This works well, I'm going to select your answer. Thanks!
  • @GengisKhan Does this answer your question? It's a copy of what you already have in your question.
  • This might require Cast in case dogs and cats are collections of the child classes and not the Animal class.
  • Yes, currently it's not allowing me to do it as Diego suggested.
  • @GengisKhan right, I forgot about the different types, now I have updated and tested my answer.
  • I forgot another important point, which makes this a little more complicated. Color is not a property of Animal, but both Dog and Cat have it. As a reason, the above solution doesn't work.
  • @GengisKhan That doesn't make any sense, can't you just add it to Animal? Or use an interface?