I am using some basic LINQ quries. I need to know How LINQ is efficient Than FOREACH and FOR loops and why we use LINQ, and what LINQ can replace in daily life coding.
1. Is LINQ can be used to reduce LOC.
2. What are all the function that we can do using LINQ.
3. What LINQ replaces.
Hi Community,
I am using some basic LINQ quries. I need to know How LINQ is efficient Than FOREACH and FOR loops and why we use LINQ, and what LINQ can replace in daily life coding.
1. Is LINQ can be used to reduce LOC.
2. What are all the function that we can do using LINQ.
3. What LINQ replaces.
LINQ To XMLLINQ assembly referenceLINQ Deploylinq to read xmlLinq databaselinq to entityLINQ to Entity Stored Procedurelinq listLINQ with XdocumentLINQ / JOIN / DataServiceQueryLinq DataServiceQuerylINQ distinctLINQ To SQLlinq syntaxlinq order by multipleLINQ / DataServiceQueryLinq To Sql One to Many Many To ManyLINQ XMLlinq ObservableCollection silverlight xml
To answer your question on LINQ efficiency, LINQ does have performance hit - in certain scenarios. According to this post
http://ox.no/posts/linq-vs-loop-a-performance-test, for simple loops over small values, LINQ is slower - but over larger sets, it's quicker. And declaring an explicit type instead of using var
(for a value type) causes excessing boxing and unboxing, which slows things down.
On to the questions:
1) Yes, there are cases where LINQ can make code more readable and reduce lines-of-code.
3) LINQ is intended to provide a uniform method of querying relational data regardless of source, be it in a database, XML document, or a collection. Have a look at
http://en.wikipedia.org/wiki/Language_Integrated_Query.
Thanks for yours reply it will be useful. But i have some more question then why we go for LINQ is there is any benefit in using this. Only for reducing LOC we can't move to LINQ.
Is there is any other benefits in that can you explain me some more about LINQ and its usage.
If you measure efficiency in lines of code then you are on the wrong path. Most of the cost of an application comes from programmer maintenance, changing functions. Is the idiom clear and more readable then you will save money.
That being said there is a fixed ratio of bugs per lines of code for a programmer (ratios of individuals vary). The more lines of code the more bugs. I might do 5 bugs per 1000, if I knock off 2000 lines of code I have stopped 10 bugs, overall saving
in debugging time.
I have run a complete Linq system with Objects and datasets on very minimal hardware and I have never complained about performance. This was old laptops scoring sport at a venue.
Efficiency is a strange thing, what you perceive as potentially inefficient is not in practice. Simply put a cheap computer will probably perform OK on most applications. If you have large sets of data then a Database will optimise you queries (which
can be bound to Linq using Linq to Entities or Linq to SQL) and therefore the resulting dataset you actually manipulate in code are relatively small and the true cost of manipulating that data is relatively small.
In my opinion unless you are doing complex analysis of data (stock analysis) then forget about performance. If performance is an issue then measure it, don't assume you can out guess a good profiler. I speak from experience, I optimised code that was
5% of the runtime once, 50% improvement on 5% is nothing. 10% improvement on 95% is much more significant.
Linq is the single reason I moved to C#. It makes working with data very clear.
NavinKumar.K.S
Participant
864 Points
471 Posts
Efficiency of LINQ
Oct 27, 2010 06:26 AM | LINK
Hi Community,
I am using some basic LINQ quries. I need to know How LINQ is efficient Than FOREACH and FOR loops and why we use LINQ, and what LINQ can replace in daily life coding.
1. Is LINQ can be used to reduce LOC.
2. What are all the function that we can do using LINQ.
3. What LINQ replaces.
LINQ To XML LINQ assembly reference LINQ Deploy linq to read xml Linq database linq to entity LINQ to Entity Stored Procedure linq list LINQ with Xdocument LINQ / JOIN / DataServiceQuery Linq DataServiceQuery lINQ distinct LINQ To SQL linq syntax linq order by multiple LINQ / DataServiceQuery Linq To Sql One to Many Many To Many LINQ XML linq ObservableCollection silverlight xml
NavinKumar.K.S
ksnavinkumar@live.com
rabbott
Contributor
3426 Points
473 Posts
Re: Efficiency of LINQ
Oct 27, 2010 11:36 AM | LINK
Hi NavinKumarKS,
To answer your question on LINQ efficiency, LINQ does have performance hit - in certain scenarios. According to this post http://ox.no/posts/linq-vs-loop-a-performance-test, for simple loops over small values, LINQ is slower - but over larger sets, it's quicker. And declaring an explicit type instead of using var (for a value type) causes excessing boxing and unboxing, which slows things down.
On to the questions:
1) Yes, there are cases where LINQ can make code more readable and reduce lines-of-code.
2) Have a look at http://msdn.microsoft.com/en-us/vcsharp/aa336746.aspx for samples of LINQ functions.
3) LINQ is intended to provide a uniform method of querying relational data regardless of source, be it in a database, XML document, or a collection. Have a look at http://en.wikipedia.org/wiki/Language_Integrated_Query.
Regards,
Rob
NavinKumar.K.S
Participant
864 Points
471 Posts
Re: Efficiency of LINQ
Oct 27, 2010 11:53 AM | LINK
Hi rabbott,
Thanks for yours reply it will be useful. But i have some more question then why we go for LINQ is there is any benefit in using this. Only for reducing LOC we can't move to LINQ.
Is there is any other benefits in that can you explain me some more about LINQ and its usage.
NavinKumar.K.S
ksnavinkumar@live.com
waratah
Member
93 Points
22 Posts
Re: Efficiency of LINQ
Oct 28, 2010 12:35 AM | LINK
If you measure efficiency in lines of code then you are on the wrong path. Most of the cost of an application comes from programmer maintenance, changing functions. Is the idiom clear and more readable then you will save money.
That being said there is a fixed ratio of bugs per lines of code for a programmer (ratios of individuals vary). The more lines of code the more bugs. I might do 5 bugs per 1000, if I knock off 2000 lines of code I have stopped 10 bugs, overall saving in debugging time.
I have run a complete Linq system with Objects and datasets on very minimal hardware and I have never complained about performance. This was old laptops scoring sport at a venue.
Efficiency is a strange thing, what you perceive as potentially inefficient is not in practice. Simply put a cheap computer will probably perform OK on most applications. If you have large sets of data then a Database will optimise you queries (which can be bound to Linq using Linq to Entities or Linq to SQL) and therefore the resulting dataset you actually manipulate in code are relatively small and the true cost of manipulating that data is relatively small.
In my opinion unless you are doing complex analysis of data (stock analysis) then forget about performance. If performance is an issue then measure it, don't assume you can out guess a good profiler. I speak from experience, I optimised code that was 5% of the runtime once, 50% improvement on 5% is nothing. 10% improvement on 95% is much more significant.
Linq is the single reason I moved to C#. It makes working with data very clear.
Yes it does reduce lines of code.
NavinKumar.K.S
Participant
864 Points
471 Posts
Re: Efficiency of LINQ
Dec 08, 2010 07:16 AM | LINK
Hi all this is a link that i got
How LINQ works internally?
NavinKumar.K.S
ksnavinkumar@live.com