D
DaveD
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
"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