LINQ is a wonderful thing that has great potential. All interested programmers know LINQ to SQL or LINQ to XML. I would like to expand this list and give a simple example of another LINQ application. Take for example an asp.net control like CheckBoxList. This element allows you to display a set of checkboxes united by some common sense. For example, we can display a list of banks, giving the user the opportunity to create the necessary set in a certain context.
The task will be to obtain a list of bank identifiers that the user has selected. In the simplest case, we could go over the collection of items and select the ones that the user noted. But LINQ makes this process even easier:
var Banks = cblBanks.Items.Cast <ListItem> (). Where (x => x.Selected == true ) .Select (y => Convert .ToInt32 (y.Value)); * This source code was highlighted with Source Code Highlighter .
Very simple and elegant, is not it? I would like to note that LINQ can be useful for similar work not only with controls, but also with any non-generic collection via Enumerable.Cast (TResult) or the similar method Enumerable.OfType (TResult). ')
PS: “there are no errors in the article!” - unfortunately, this is impossible to say, but I will be glad if you pay my attention to the error you found.