0

(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.

  1. How does it do what it does?
  2. Is there be a better/more straightforward way?

Thanks.

Sabuncu
  • 5,095
  • 5
  • 55
  • 89
  • You're mixing query- and method syntax. Btw, why do you habe a class `Word` with a single property `-word` at all? Also, please follow [.NET Naming Conventions](http://msdn.microsoft.com/en-us/library/ms229012(v=vs.110).aspx). – Tim Schmelter Aug 09 '14 at 19:41
  • @TimSchmelter The class contains other properties as I have shown with a comment - I did not detail them here. Also, could you please expand on your comment re: query/method syntax? Which is which? – Sabuncu Aug 09 '14 at 19:44
  • Query-syntax: `var query = from item in collection` method-syntax: `var query = collection.Select(item => item)`. Here's a question about the pros and cons: http://stackoverflow.com/questions/214500/linq-fluent-and-query-expression-is-there-any-benefits-of-one-over-other – Tim Schmelter Aug 09 '14 at 19:55

1 Answers1

1

You could achieve that you want more simple like below:

IEnumerable<Word> subset = items.Where(x => x._word.Contains("r"));

With the above linq query, you filter the collection called items using an extension method called Where. Each element in the collection of items that satisfies the filter in the Where method, it will be contained on the subset. Inside the Where you have defined your fitler, using a lambda expression. On the left side of => you have your input -the random element of items, while on the right side of => you have your output, which in your case will be either true or false. On the rigth side of =>, you usethe string's method called Contains, which in simple terms checks if the string you pass as a parameter to this method is contained in the property called _word, of the element x.

Christos
  • 53,228
  • 8
  • 76
  • 108
  • Yes, this works, thank you. But is `Where` an extension method? I was under the impression that extension methods are those that you define yourself. Sorry if this is a stupid question. – Sabuncu Aug 09 '14 at 19:47
  • `Where` is an extension method defined in the `Enumerable`-class, but @Christos is wrong regarding the `Contains` method, it is a normal member method of the `String` class. You can read about extension methods here: http://msdn.microsoft.com/en-us/library/bb383977.aspx – frank koch Aug 09 '14 at 19:57
  • @frankkoch oops you are correct. Thanks for pointing this out. – Christos Aug 09 '14 at 20:09