score:7
Accepted answer
You're probably looking for something like this:
var record = myList.All(m => m.Date.Equals("Never"))
? myList.FirstOrDefault()
: myList.FirstOrDefault(m => !m.Date.Equals("Never"));
For more on .All()
just look at this MSDN post.
score:8
var record = myList.FirstOrDefault(m => !m.Date.Equals("Never"))
?? myList.FirstOrDefault();
i.e. first one that doesn't equal never or null, and if null, just the first one (or null).
score:-1
Since you have two rules, you will need an if condition somewhere.
The simplest form that I can think of is something like this:
return myList.All(x => x.Date == "Never") ? myList.FirstOrDefault() : myList.FirstOrDefault(x => x.Date != "Never");
Source: stackoverflow.com
Related Articles
- Linq Query with a Where clause in an Include statement
- Linq with where clause in many-to-many EF Code First object
- Where clause with if statement
- How to query by where clause with EF code first
- Entity Framework - LINQ statement with WHERE clause based on NULLABLE parameter
- C# - Linq optimize code with List and Where clause
- LINQ Statement with where clause makes the execution slow
- How to convert multiple SQL LEFT JOIN statement with where clause to LINQ
- Expressing IN sql statement in Linq with computed string in where clause
- LINQ where clause with an if statement
- Linq statement fails with dynamic where clause
- EF LINQ to SQL is getting whole table from DB instead of selection - where clause contains list with Any statement
- Entity Framework dynamic linq where from generic source with dynamic where clause
- lambda expression join multiple tables with select and where clause
- System.Linq.Dynamic - Can I use IN clause in WHERE statement
- Equivalence of query and method (lambda) syntax of a Join with Where clause
- Why C# LINQ expressions must end with Select or Group By Clause where as no such restriction in VB.Net
- Linq-to-Entities: LEFT OUTER JOIN with WHERE clause and projection
- LINQ Where clause with Contains where the list has complex object
- How can i write SQL update query with where clause in Entity Framework in C#
- LINQ query with a WHERE clause with multiple conditions
- Linq query works with null but not int? in where clause
- SELECT AS in Linq with WHERE clause
- Linq where clause with multiple conditions and null check
- Entity Framework Core where clause with multiple values
- Dynamic Where Clause over relational tables with LINQ to SQL
- C# DynamicLinq where clause with Any()
- Linq to SQL: Where clause comparing a Nullable<DateTime> with a SQL datetime null column
- Linq where clause with multiple conditions
- linq query with dynamic predicates in where clause joined by OR
- LINQ, use string[] in a query
- Get the index of a partially matching item in a list c# using linq
- IEnumerable<XElement> and foreach
- passing list to linq query
- Filtering list using linq and mvc
- Multiple text box search ASP.NET MVC
- Sum of 2 values in same lambda?
- Error on result of linq expression (and anounymous type)
- Why is there a difference between using a lambda in a where extension method an using a func in a where extension method
- Get selected element by combobox from XML file
- order by how many matches in PredicateBuilder
- How to remove null datarow from datatable except Autoincrement column
- How do I avoid using nested foreach statements by using lambda or linq in this scenario?
- Joining to Lists via LINQ
- Getting the max value of enum via LINQ and only given the enum type
- LinQ Query Dynamic Conditions List
- Linq Data Can Not Be Null By Using Stored Procedure
- Linq query to execute stored procedure with return type as DataTable
- How to get specific element Count in XML or XElement variable
- default values in LINQ query