473,804 Members | 3,037 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How does LINQ exploit sorted collections?

Some collections are such that efficient search algorithms work on them
such as binary search if the collection is a type which is sorted.

I'm wondering how LINQ searches these collections and if it takes
advantage of the fact that some collections are sorted?

We were speculating that the standard .net 2.0 collections might
implement the IQueryable interface for those collections where there
are more efficient search algorithms.

What would seem awful is if it searched through all objects only by
iteration, which would always be a sequential search.

I did try a Google but didn't come up with anything relevant, so please
don't flame me if you found something. Just because my choice of
keywords may have been bad is no reason to get angry.

Thanks.

Jan 23 '07 #1
5 2639
Hi,

I think LINQ just iterates over the collection. Doesn't it just use
IEnumerable<T(o r ICollection<Tif certain methods exist)? - though I
could be wrong.

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"WebSnozz" <sh******@cs.fs u.eduwrote in message
news:11******** *************@s 48g2000cws.goog legroups.com...
Some collections are such that efficient search algorithms work on them
such as binary search if the collection is a type which is sorted.

I'm wondering how LINQ searches these collections and if it takes
advantage of the fact that some collections are sorted?

We were speculating that the standard .net 2.0 collections might
implement the IQueryable interface for those collections where there
are more efficient search algorithms.

What would seem awful is if it searched through all objects only by
iteration, which would always be a sequential search.

I did try a Google but didn't come up with anything relevant, so please
don't flame me if you found something. Just because my choice of
keywords may have been bad is no reason to get angry.

Thanks.

Jan 23 '07 #2
Hi,

On second thought, what's to stop a LINQ expression from containing an
explicit call to BinarySearch, for example?

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"WebSnozz" <sh******@cs.fs u.eduwrote in message
news:11******** *************@s 48g2000cws.goog legroups.com...
Some collections are such that efficient search algorithms work on them
such as binary search if the collection is a type which is sorted.

I'm wondering how LINQ searches these collections and if it takes
advantage of the fact that some collections are sorted?

We were speculating that the standard .net 2.0 collections might
implement the IQueryable interface for those collections where there
are more efficient search algorithms.

What would seem awful is if it searched through all objects only by
iteration, which would always be a sequential search.

I did try a Google but didn't come up with anything relevant, so please
don't flame me if you found something. Just because my choice of
keywords may have been bad is no reason to get angry.

Thanks.

Jan 23 '07 #3
What would stop it I think(I say I think cause I don't know much about
LINQ) is that only certain data structures would support BinarySearch,
and LINQ working on any structure that implements IQueryable means that
isn't guaranteed. All LINQ sees(I think) is what is available in
IQueryable and IEnumerable. So it wouldn't know to call BinarySearch,
cause it doesn't know it exists, nor should it. But it would seem
there should be some mechanism that allows the developer of the
collection to specify how the collection is searched.

How much control does the developer have over how IQueryable operates?
If you can create a concrete implementation of IQueryable for the
collection, then inside IQueryable you could write the query executing
code such that it exploits the advantages of the data structure when
doing searches. You'd probably have to evaluate the query being run to
some extent in order to know how to perform the search.

I don't know anything about IQueryable though, so I'm not sure. But if
it is true that IQueryable can be implemented in that way, then the
next question is, does Microsoft write efficient IQueryable
implementations for all of their collections provided through the .net
framework, or do they just iterate through the collection. I guess
only MS staff or someone delving into the MS assemblies would be able
to determine that.

I guess I could read up a bunch on IQueryable and see if that was
possible. But still that wouldn't answer the above regarding how
standard collections implement IQueryable.

Thanks for the replies.

On Jan 23, 5:53 pm, "Dave Sexton" <dave@jwa[remove.this]online.com>
wrote:
Hi,

On second thought, what's to stop a LINQ expression from containing an
explicit call to BinarySearch, for example?

--
Dave Sextonhttp://davesexton.com/bloghttp://www.codeplex.co m/DocProject(Sand castle in VS IDE)

"WebSnozz" <shuma...@cs.fs u.eduwrote in messagenews:11* *************** *****@s48g2000c ws.googlegroups .com...
Some collections are such that efficient search algorithms work on them
such as binary search if the collection is a type which is sorted.
I'm wondering how LINQ searches these collections and if it takes
advantage of the fact that some collections are sorted?
We were speculating that the standard .net 2.0 collections might
implement the IQueryable interface for those collections where there
are more efficient search algorithms.
What would seem awful is if it searched through all objects only by
iteration, which would always be a sequential search.
I did try a Google but didn't come up with anything relevant, so please
don't flame me if you found something. Just because my choice of
keywords may have been bad is no reason to get angry.
Thanks.
Jan 24 '07 #4
Hi,

Yes, that was ignorant. It's been a while since I played around with LINQ
:p

--
Dave Sexton
http://davesexton.com/blog
http://www.codeplex.com/DocProject (Sandcastle in VS IDE)

"WebSnozz" <sh******@cs.fs u.eduwrote in message
news:11******** **************@ l53g2000cwa.goo glegroups.com.. .
What would stop it I think(I say I think cause I don't know much about
LINQ) is that only certain data structures would support BinarySearch,
and LINQ working on any structure that implements IQueryable means that
isn't guaranteed. All LINQ sees(I think) is what is available in
IQueryable and IEnumerable. So it wouldn't know to call BinarySearch,
cause it doesn't know it exists, nor should it. But it would seem
there should be some mechanism that allows the developer of the
collection to specify how the collection is searched.

How much control does the developer have over how IQueryable operates?
If you can create a concrete implementation of IQueryable for the
collection, then inside IQueryable you could write the query executing
code such that it exploits the advantages of the data structure when
doing searches. You'd probably have to evaluate the query being run to
some extent in order to know how to perform the search.

I don't know anything about IQueryable though, so I'm not sure. But if
it is true that IQueryable can be implemented in that way, then the
next question is, does Microsoft write efficient IQueryable
implementations for all of their collections provided through the .net
framework, or do they just iterate through the collection. I guess
only MS staff or someone delving into the MS assemblies would be able
to determine that.

I guess I could read up a bunch on IQueryable and see if that was
possible. But still that wouldn't answer the above regarding how
standard collections implement IQueryable.

Thanks for the replies.

On Jan 23, 5:53 pm, "Dave Sexton" <dave@jwa[remove.this]online.com>
wrote:
>Hi,

On second thought, what's to stop a LINQ expression from containing an
explicit call to BinarySearch, for example?

--
Dave
Sextonhttp://davesexton.com/bloghttp://www.codeplex.co m/DocProject(Sand castle
in VS IDE)

"WebSnozz" <shuma...@cs.fs u.eduwrote in
messagenews:11 *************** ******@s48g2000 cws.googlegroup s.com...
Some collections are such that efficient search algorithms work on them
such as binary search if the collection is a type which is sorted.
I'm wondering how LINQ searches these collections and if it takes
advantage of the fact that some collections are sorted?
We were speculating that the standard .net 2.0 collections might
implement the IQueryable interface for those collections where there
are more efficient search algorithms.
What would seem awful is if it searched through all objects only by
iteration, which would always be a sequential search.
I did try a Google but didn't come up with anything relevant, so please
don't flame me if you found something. Just because my choice of
keywords may have been bad is no reason to get angry.
Thanks.

Jan 24 '07 #5
WebSnozz wrote:
Some collections are such that efficient search algorithms work on them
such as binary search if the collection is a type which is sorted.

I'm wondering how LINQ searches these collections and if it takes
advantage of the fact that some collections are sorted?
They would have to implement the appropriate methods and decompose the
predicate to see if it consists only of operators (==, <, etc.) that
work well with the internal structure.
We were speculating that the standard .net 2.0 collections might
implement the IQueryable interface for those collections where there
are more efficient search algorithms.
I can see a possible case for some collections implementing
Where<T>(Expres sion<Func<T,boo l>predicate) and friends (OrderBy,
OrderByDescendi ng), but I'm not sure if the gain would be worth the cost
of implementing them.

We'll know more when it's closer to release.

-- Barry

--
http://barrkel.blogspot.com/
Jan 25 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
2508
by: Peter Olcott | last post by:
Does the 2005 release of Visual Studio provide either STL (the C++ Standard Template Library) or an equivalent for C#? I am most interested in the 2005 C# equivalent of std::vector.
4
2394
by: Olivier Matrot | last post by:
Hello, I'm still using old style strongly typed collections (derived from CollectionBase .NET 1.1) and I would like to be able to query in memory collection with LINQ in the future. I also would like to improve performance of the collection avoiding boxing/unboxing of objects during access of items in the collection. Could you please point me to the right direction ? TIA.
1
1347
by: Bob Johnson | last post by:
Just wondering if LINQ might be useful and appropriate in the following scenario: I'm writing a Windows Forms app that enables the user to search for a "client account". The client account is made up of a client number, plus a case number. A given client can have many cases. Here are two clients - the first of which has two cases. 1234-897 1234-989 7654-232
5
1669
by: =?Utf-8?B?cmF1bGF2aQ==?= | last post by:
linq on objects... want to find if the object exist in its collection if I have a loop searching in the collection foreach (myClass r in collections) { if (r.field01 == type && r.field02 == id) return true;
3
1928
by: Vivien Parlat | last post by:
Hello, I am currently using VB.Net 2008 express. I use linq to perform queries on a database, and I'm using the following link's source to convert those queries into DataTables i can then bind to WinForms' DataGridViews: http://blogs.msdn.com/aconrad/archive/2007/09/07/science-project.aspx The point is, the properties names extracted by the following lines:
9
2264
by: Cirene | last post by:
I'm about to begin a brand new, big, ASP.NET project (using 3.5 .net fw), VS 2008. I'm using MySQL as the backend (customer request.) I have absolutely no experience with LINQ and/or the Entity Framework. Though I am quite comfortable with ADO.NET and VB.NET. In your opinion, should I take a few days and learn it and utilize these technologies in this new project (I'm starting from scratch)? Are the benefits worth it in your opinion?...
21
4369
by: hrishy | last post by:
Hi Will LINQ be ported to Python ? regards Hrishy
4
2537
by: George | last post by:
I am a bit conservative type and usually give some time for technology to mature before starting to try it. Today my question is Linq. To start using it or not. So here is the voting questions. 1. It speeds up development. Yes or No? 2. It makes programs easier to code and read. Yes or No? 3. Perfomance is the same (or comparable) comparing Linq with MsSql and ADO.NET
1
8684
by: alex21 | last post by:
Ok i am trying to use a Linq query to access a dictionary. public static Dictionary<string, Client> Clients = new Dictionary<string, Client>();Using this Linq query: IEnumerable<Staff> loginquery = from staff in Database.Staff where staff.Value.Passcode == txt_password.Text select staff;but i get the error Error 2 Cannot implicitly convert type...
0
9706
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9579
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10317
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10075
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
7615
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6851
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5651
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3815
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2990
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.