Many things in LINQ can be accomplished without the library. But for some scenarios, LINQ is most appropriate.
Examples are:
SELECT
– https://stackoverflow.com/questions/11883262/wrapping-list-items-inside-div-in-a-repeaterSelectMany
,Contains
– https://stackoverflow.com/questions/11778979/better-code-pattern-for-checking-existence-of-valueEnumerable.Range
– https://stackoverflow.com/questions/11780128/scalable-c-sharp-code-for-creating-array-from-config-fileWHERE
https://stackoverflow.com/questions/13171850/trim-string-if-a-string-ends-with-a-specific-word
What factors to take into account when deciding between LINQ and regular .Net language elements?
8
I’ve found LINQ to be handy in literally any scenario where I’m working with collections of objects. The main things you do with collections of objects are:
- Search them for items matching some characteristic
- Traverse them to calculate some aggregate value
- Visit each item in the collection and calculate some value based on it (translation)
- Rearranging its contents for some purpose (transformation)
Each of these scenarios has a number of LINQ methods that are designed to help with the task:
- For searching, there’s
Where()
,First()
,Any()
,All()
,Last()
- For aggregation, there’s
Aggregate()
,Average()
Sum()
,Max()
,Min()
- For translation there’s
Select()
andSelectMany()
- For transformation there’s
OrderBy()
,GroupBy()
,Reverse()
And a few other odds and ends that I’m not sure how to categorize in the above for – for example the set operations Union()
and Intersect()
.
In general, all of these functions do the work of hand-coded loops, but have the advantage of improving legibility because they help to replace several lines worth of code with a few LINQ operations that are named after what they do. For example, consider
string[] input;
var output = new string[input.Length];
for(int i = 0; i < input.Length; i++)
{
output[input.Length - i - 1] = input[i];
}
versus:
var output = input.Reverse().ToArray();
Another advantage is that it’s easy to string them together to get more complex behavior. Say that instead you wanted to get only the unique values in the array, and sort them. Right now I’m feeling too lazy to hand-code a solution to it, but I’m sure whatever it is it would be much longer and harder to read than
var output = input.Distinct().OrderBy(x=>x).ToArray();
And it definitely would take longer, which is why I don’t feel like writing it.
There are a number of different types of technologies which fall under the umbrella term of LINQ:
LINQ-to-Objects
LINQ-to-SQL
LINQ-to-Datasets
LINQ-to-XML
LINQ-to-Entities
And there are more provided by others as well, such as LINQ-to-CSV, LINQ-to-NHibernate, etc.
In every one of these cases, the purpose of LINQ is the same: to query sets of data. So anytime you have some sort of set (0, 1 or more items) of a particular type of data, LINQ is a succinct solution to deal with it. Many questions seem to arise over whether it performs better than a hand-written approach, but that’s not the point. LINQ specifies intent rather than implementation.