Answers for "select lists from list linq c#"

C#
3

c# linq select from object list

// this will return the first correct answer,
// or throw an exception if there are no correct answers
var correct = answers.First(a => a.Correct); 

// this will return the first correct answer, 
// or null if there are no correct answers
var correct = answers.FirstOrDefault(a => a.Correct); 

// this will return a list containing all answers which are correct,
// or an empty list if there are no correct answers
var allCorrect = answers.Where(a => a.Correct).ToList();
Posted by: Guest on March-18-2020
-1

linq from list c#

// Single
string search = "testSearch";
 List<string> list = new List<string>(); // Need to create a string list 
 string result = list.Single(s => s == search);

// Find all where meets the criteria
IEnumerable<string> results = list.Where(s => s == search);
// Select first one
string result = list.First(s => s == search);
Posted by: Guest on July-11-2021

C# Answers by Framework

Browse Popular Code Answers by Language