Click here to Skip to main content
Click here to Skip to main content

Tagged as

Go to top

LINQ is for querying! Not for manipulation.

, 5 Dec 2011
Rate this:
Please Sign up or sign in to vote.
Notes regarding LINQ abstraction.

LINQ is all about querying data, not manipulating data.

Manipulate data means to change the data, for example:

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

An example of querying data:

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)

Share

About the Author

Delashmate
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... Pinmembervl212-Dec-11 22:48 
GeneralReason for my vote of 1 No value added PinmemberArchAngel12312-Dec-11 10:28 
GeneralRe: This tip allows you to understand better the abstraction of ... PinmemberDelashmate12-Dec-11 22:14 

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

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

| Advertise | Privacy | Mobile
Web02 | 2.8.140926.1 | Last Updated 5 Dec 2011
Article Copyright 2011 by Delashmate
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid