I'm curious about the overall LINQ performance. I admit this will come in handy, but how does LINQ work? I know this is a broad question. So I want to ask about a specific example:
I have an anonymous type:
var users = reader.Select(user => new MembershipUser(reader.Name, reader Age));
And now I want to convert it to MembershipUserCollection.
MembershipUserCollection
So, I do it like this:
MembershipUserCollection membershipUsers = new MembershipUserCollection(); users.ToList().ForEach(membershipUsers.Add); //what is the complexity of this line?
What is the complexity of the last line? Is it n ^ 2?
Does the ToList () method iterate for each user item and add it to the list? Or does ToList () work differently? Because if this is not so, I can hardly judge the reason for using the last line of code, and not just:
foreach (var user in users) { membershipUsers.Add(user); }
It O (n) - .ToList() List<T> ( O (1)). , .
.ToList()
List<T>
, , , , List<T>, ( ).
, - .ForEach() - List<T> s. , IEnumerable<T> s, -
.ForEach()
IEnumerable<T>
foreach (var user in users) membershipUsers.Add(user)
; -)
, ToList() , , LINQ. ToList() - , . LINQ - , LINQ . LINQ2SQL , , , .
, , , , , , . , ForEach , , , . , ( ). LINQ , , , LINQ.
ForEach
, LINQ , LINQ. , . , LINQ , , LINQ. . , , , LINQ. , .
, , , LINQ , , . - , .
, , , , . - O (n).
The performance of using ForEach in the list compared to the foreach loop comes down to the overhead of calling a delegate and the overhead of creating and using an enumerator, I can’t say which one is faster, but if both are used in the -memory list, the complexity is the same .
Source: https://habr.com/ru/post/1771167/More articles:Ruby and Resque Error Segmentation Capture on Linux - segmentation-faultSilverlight (card game) and WCF connection (with IIS) - silverlightSwitch assembly language - assemblyHow to choose the right disk size - c #Как высмеять метод экземпляра уже издевавшегося объекта? - rubyThe fastest way to deserialize objects from a huge binary file is .netGrails / AJAX: updating an arbitrary region on a page using g: submitToRemote - ajaxRecognized numbers using PHP - phpC - What is this syntax about? << - cReading multi-line text with values separated by spaces - javaAll Articles