Click here to Skip to main content
15,881,172 members
Articles / Programming Languages / C#
Tip/Trick

LINQ is for querying! Not for manipulation.

Rate me:
Please Sign up or sign in to vote.
2.50/5 (2 votes)
5 Dec 2011CPOL 22.9K   3   3
Notes regarding LINQ abstraction.

LINQ is all about querying data, not manipulating data.


Manipulate data means to change the data, for example:


C#
List<string> lst = new List<string>();
lst.Add("1"); //this line is changing(manipulate) lst

An example of querying data:


C#
List<string> lst = new List<string>();
var res = lst.where(s => s.StartsWith("1"); //this line is NOT modify lst

Query data is mostly used by using restriction and Projection operators


So now, after we have clarified the distinction between manipulating and querying, let us understand the motivation behind each..


Well, the answer is that LINQ extends IEnumerable*, and IEnumerable is readonly! Therefore manipulating data with IEnumerable will break the abstraction of it.


Then you might ask, why does LINQ extend IEnumerable and not other interfaces which allow to manipulate data?


This is beacause it allows LINQ query operators to be cohesive and focus on the main goal which is to query data.
LINQ operators don't care if the collection is readonly or not, adding new functionality to manipulate data will break this abstraction.


The discussion of why LINQ extends IEnumerable/ the benefits and disadvantages of it, is a much wider topic, which I am going to leave open for now..


I leave it to you to wonder why extending IEnumeurable supports the abstraction of LINQ Deferred Execution.


(*Reminder: The IEnumerator interface exposes the Current property, and the Reset and MoveNext methods.)

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer GE
Israel Israel
I am Software Developer at GE company for more than 2 years,

I created LINQTutorial.net since I didn't found any other good dedicate LINQ tutorial

Comments and Discussions

 
GeneralProbably you did not get the idea of the functional programm... Pin
vl212-Dec-11 22:48
vl212-Dec-11 22:48 
GeneralReason for my vote of 1 No value added Pin
ArchAngel12312-Dec-11 10:28
ArchAngel12312-Dec-11 10:28 
GeneralRe: This tip allows you to understand better the abstraction of ... Pin
Delashmate12-Dec-11 22:14
Delashmate12-Dec-11 22:14 
This tip allows you to understand better the abstraction of LINQ,
I agree with you, that you can't imply value from it directly

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.