(Disclaimer: I am new to LINQ, and I did do my homework, but all my research came up with examples that deal with strings directly. My code deals with an ObservableCollection
of objects where each object contains a string.)
I have the following code:
public class Word
{
public string _word { get; set; }
// Other properties.
}
ObservableCollection<Word> items = new ObservableCollection<Word>();
items.Add(new Word() { _word = "Alfa" });
items.Add(new Word() { _word = "Bravo" });
items.Add(new Word() { _word = "Charlie" });
I am trying to find the words that have an "r" in them:
IEnumerable<Word> subset = from ii in items select ii;
subset = subset.Where(p => p._word.Contains("r"));
The above works (I get 'Bravo' and 'Charlie').
My question: I have devised the above LINQ query from bits and pieces of examples I found online/in books.
- How does it do what it does?
- Is there be a better/more straightforward way?
Thanks.