score:0
i recently found myself wondering whether i've been totally spoiled by linq. yes, i now use it all the time to pick all sort of things out from all sort of collections.
score:0
i started to, but found out in some cases, i saved time by using this approach:
for (var i = 0, len = list.count; i < len; i++) { .. }
not necessarily in all cases, but some. most extension methods use the foreach approach of querying.
score:0
i try to follow these rules:
- whenever i'm just querying (filtering, projecting, ...) collections, use linq.
- as soon as i'm actually 'doing' something with the result (i.e, introduce side effects), i'll use a for loop.
so in this example, i'll use linq.
also, i always try to split up the 'query definition' from the 'query evaluation':
dim query = from r in table.asenumerable()
select r.field(of integer)("id")
dim result = query.tolist()
this makes it clear when that (in this case in-memory) query will be evaluated.
score:1
i avoid linq unless it helps readability a lot, because it completely destroys edit-and-continue.
when they fix that, i will probably start using it more, because i do like the syntax a lot for some things.
score:1
for almost everything i've done i've come to the conclusion that linq is optimized enough. if i handcrafted a for loop it would have better performance, but in the grand scheme of things we are usually talking milliseconds. since i rarely have a situation where those milliseconds will make any kind of impact, i find it's much more important to have readable code with clear intentions. i would much rather have a call that is 50ms slower than have someone come along and break it altogether!
score:1
resharper has a cool feature that will flag and convert loops into linq expressions. i will flip it to the linq version and see if that hurts or helps readability. if the linq expression more clearly communicates the intent of the code, i will go with that. if the linq expression is unreadable, i will flip back to the foreach version.
most of the performance issues don't really compare with readability for me.
clarity trumps cleverness.
in the above example, i would go with the the linq version since it clearly explains the intent and also locks out people accidently adding side effects in the loop.
score:5
whenever possible i favor the declarative way of programming instead of imperative. when you use a declarative approach the clr can optimize the code based on the characteristics of the machine. for example if it has multiple cores it could parallelize the execution while if you use an imperative for loop you are basically locking this possibility. today maybe there's no big difference but i think that in the future more and more extensions like plinq will appear allowing better optimization.
Source: stackoverflow.com
Related Query
- Do you choose Linq over Forloops?
- How do you handle arbitrary namespaces when querying over Linq to XML?
- What would you choose for your data layer today, Linq or Subsonic?
- LINQ Source Code Available
- linq - how do you do a query for items in one query source that are not in another one?
- creating Linq to sqlite dbml from DbLinq source code
- Using Linq to build a graph class; can you make this code look better?
- source code for LINQ 101 samples
- c# Linq or code to extract groups from a single list of source data
- How do you perform a left outer join using linq extension methods
- Convert string[] to int[] in one line of code using LINQ
- Code equivalent to the 'let' keyword in chained LINQ extension method calls
- How do you perform a CROSS JOIN with LINQ to SQL?
- Linq code to select one item
- How do you add an index field to Linq results
- How are people unit testing code that uses Linq to SQL
- How can you handle an IN sub-query with LINQ to SQL?
- How do you construct a LINQ to Entities query to load child objects directly, instead of calling a Reference property or Load()
- How to perform LINQ query over Enum?
- Can you use LINQ types and extension methods in IronPython?
- How do you use LINQ to find the duplicate of a specific property?
- How to use DATEADD over column in LINQ - DateAdd is not recognized by LINQ
- How do you use LINQ with Sqlite
- How do you add dynamic 'where' clauses to a linq query?
- Is there an IEnumerable implementation that only iterates over it's source (e.g. LINQ) once?
- LINQ query to perform a projection, skipping or wrapping exceptions where source throws on IEnumerable.GetNext()
- How would you write an Upsert for LINQ to SQL?
- How can you do custom sorting in LINQ with null always on the end?
- I am wondering about the state of connection and impact on code performance by 'yield' while iterating over data reader object
- Can you reverse order a string in one line with LINQ or a LAMBDA expression
More Query from same tag
- Fastest way to select distinct values from list based on two properties
- How to assign value from properties to enum in Linq
- Will this XML parsing work?
- Return a list of posts where Blog.ID == id in c#
- Unable to Apply Linq Where Clause to Linked Entity in Entity Framework Core
- How to group by column in LINQ?
- How to merge arrays of objects into other objects
- How to Append to an expression
- How to seperate string by commas and colons using linq?
- C# Linq Find duplicates with multiple group by
- VB.NET LINQ Join 2 tables , then group by a column then sum
- LINQ - Find certain Item in the List<T> By Count criteria
- Return different objects with one LINQtoSQL statement
- Linq join condition with multipe data from two table
- Moq setup not returning expected object, mismatched invocation
- How to get xml log elements by searching the specific string/text using LINQ
- Implicit Cast not happening in Expression Tree
- Where can I find Linq ThenInclude method?
- Complex GroupBy in C# LINQ lambda expressions
- Merge items in list with the same key/property
- How do I get the max ID with Linq to Entity?
- Error: Unable to cast object of type 'WhereSelectEnumerableIterator`2[System.Xml.Linq.XElement,System.String]' to type 'System.IConvertible'
- Select all types of employess then specific employee
- linq query dynamically add where condition?
- List<T> RemoveAll() isn't removing items
- LINQ group items into lists in anonymous type, with duplicates
- Why linq isn't grouping/counting as expected
- Odata and Select Issues
- Force "TOP 100 PERCENT" in EF "sub-query" queryable
- Select data from different object in linq query