C# Select From List Linbque Code Example


Example 1: 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();

Example 2: c# linq list select


List<Model> newList = list.Where(m => m.application == "applicationname")
.Select(m => new Model {
application = m.application,
users = m.users.Where(u => u.surname == "surname").ToList()
}).ToList();

Comments

Popular posts from this blog

Converting A String To Int In Groovy

"Cannot Create Cache Directory /home//.composer/cache/repo/https---packagist.org/, Or Directory Is Not Writable. Proceeding Without Cache"

Android How Can I Convert A String To A Editable