Answers for "linq select object from list by property"

C#
2

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
0

get one property from list of objects linq

var result = from x in Contacts
             where x.ContactTypeID == 2 && x.OrganizationName == "COMPANY XYZ"
             select x.ContactID;
Posted by: Guest on January-08-2021

Code answers related to "linq select object from list by property"

C# Answers by Framework

Browse Popular Code Answers by Language