473,378 Members | 1,518 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Creating on-the-fly lookup tables in LINQ

Let's say I have a collection ("items") of objects that each have a
"string Name" property. I understand I can create a loop like this:

foreach (string name in names) {
var item = (from x in items where name == x.Name select
x).First();
// Work on item
}

Is there a way to make that query more efficient? (My assumption is
that a linear search is performed each time and that any internal
index is lost on each iteration).

One idea I had that didn;t work, but may explain what I mean:

var itemsByName = from x in items group x by x.Name;

foreach (string name in names) {
var item = itemsByName[ name ].First();
// Work on item
}

Thanks for any pointers, and I hope you appreciate the chance to point
and laugh at the LINQ newbie :)

Regards
Dave Dawkins
Sep 9 '08 #1
1 2575
DaveD wrote:
Let's say I have a collection ("items") of objects that each have a
"string Name" property. I understand I can create a loop like this:

foreach (string name in names) {
var item = (from x in items where name == x.Name select
x).First();
// Work on item
}

Is there a way to make that query more efficient? (My assumption is
that a linear search is performed each time and that any internal
index is lost on each iteration).

One idea I had that didn;t work, but may explain what I mean:

var itemsByName = from x in items group x by x.Name;

foreach (string name in names) {
var item = itemsByName[ name ].First();
// Work on item
}
The purest expression of what you're doing is this:

var filteredItems =
from item in items
group item by item.Name into g
where names.Contains(g.Key)
select g.First();

While this does perform a linear search for every group created, this is not
necessarily a problem if "names" isn't large. Creating the lookup for
grouping will likely consume far more time (and memory).

That said, it *is* possible to avoid this with an explicit lookup:

var itemsByName = items.ToLookup(item =item.Name);
var filteredItems =
from name in names
let namedItems = itemsByName[name]
where namedItems.Any()
select namedItems.First();

This only goes through items and names once.

If "items" is very large and "names" is small, creating the lookup in the
first place may be prohibitive. Some old-fashioned imperative programming
could help there:

string[] namesArr = names.ToArray();
foreach (var item in items) {
int i = Array.IndexOf(namesArr, item.Name);
if (i < 0) continue;
namesArr[i] = null; // remove from further consideration

// work on item
}

This assumes that no item can have a null name (if they can, a separate
array of booleans could be used). If "names" is too large to make linear
searches practical, you could do the same exercise with
names.ToDictionary(name =name).

All this should be considered optimization that may not be necessary or
applicable, though. Try the LINQ versions first and see if they'll suit your
needs.

--
J.
Sep 9 '08 #2

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

Similar topics

1
by: Inpreet | last post by:
Hello I am using: ************************************************************************* header("Content-Type:application/force-download",false); header("Content-Disposition: attachment;...
1
by: Vannela | last post by:
In my unmanaged there is code i n this way Class1 c1= new Class1(); // Class1 is one object and creating a instance of it\ Class1 c2= new Class2();// Class2 is another object a...
3
by: Philip Carnstam | last post by:
Hi, Can someone lead me through creating a user object in AD. I have tried creating one through LDAP and ADSI (WinNT://) but nothing happens. DirectoryEntry DE = new...
1
by: M K | last post by:
Can I create code so that when my users create a username in an Intranet form (Using Integrated Windows security) that user gets added to AD under a certain group? Where can I get info on this?
2
by: maykut | last post by:
Hi everybody! I have a problem with creating classes in a java source code.I don't know is it possible. creating an non-existing class and filling it with its constructor java source code?After all...
1
by: maykut | last post by:
Hi everybody! I have a problem with creating classes in a java source code.I don't know is it possible. creating an non-existing class and filling it with its constructor in another java source...
1
by: vijay | last post by:
HI all, I have done a C#.net windows application and used Microsoft Excel 11.0 object library.My problem is while creating setup of my project it include Excel.exe which has big size.But...
0
by: =?Utf-8?B?TWFyaw==?= | last post by:
Users of an in-house application we have written randomly get an Error creating window handle exception, and we've not been able to determine why this happens. A typical callstack is as follows: ...
4
by: reyalberto | last post by:
I am interested in creating an online appointment page where my clients can login and set an appointment with me. I'm somewhat familiar with PHP, but would like to know how should I go for creating...
5
by: prakashsakthivel | last post by:
Hi Everyone Is any way to create self creating .exe in visual basic that means, we can create .txt file can we create .exe also same like that creating .txt file Hope I will get solution. ...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.