Hi all, first of all thank you to everyone who helped me out last night with my problems, now I need some help again.

I have a menu and submenu system, this submenu contains information about locating a driver's location, I want to be able to do a search within the if statement to find out where said Driver is located. I've attached a link to a screenshot if that helps.

http://imageshack.us/f/14/helpub.png/

I want to be able to press the number 1 (or any other number) and for the application to say the following instead of defaulting back to the depot menu:

Please enter name of driver (in this case Steven)
Then for the application to say:

"Steven is located at Depot A"

Any ideas, tips or suggestions greatly appreciated.
I don't want the coding done for me as this is cheating, I just want a pointing in the right direction

Rafa

Dani AI

Generated

Helpful summary and practical options for the menu/search flow described by . is correct that LINQ makes the lookup straightforward; the core steps are: accept the typed name, normalize the input (trim, case), run a search over depots' driver lists, and present either a single match, all matches, or a "not found" message. Two patterns below cover common needs (small lists vs. large or frequent lookups), plus a few pitfalls to watch for.

Simple, readable search (good for small collections):

// assumes Depot { string Name; List<Driver> Drivers; } and Driver { string Name; }
string input = Console.ReadLine().Trim();
var foundDepot = depots.FirstOrDefault(d =>
    d.Drivers.Any(dr => string.Equals(dr.Name, input, StringComparison.OrdinalIgnoreCase)));

if (foundDepot != null)
    Console.WriteLine(input + " is located at " + foundDepot.Name);
else
    Console.WriteLine("Driver not found");

Indexed lookup for speed (build once, reuse):

// build an index at startup: name -> list of depots (handles duplicate names)
var index = depots
  .SelectMany(d => d.Drivers.Select(dr => new { Name = dr.Name.Trim().ToLowerInvariant(), Depot = d }))
  .GroupBy(x => x.Name)
  .ToDictionary(g => g.Key, g => g.Select(x => x.Depot).ToList());

var key = input.Trim().ToLowerInvariant();
if (index.TryGetValue(key, out var depotsWithDriver)) {
  foreach (var dp in depotsWithDriver)
    Console.WriteLine(input + " is located at " + dp.Name);
} else {
  Console.WriteLine("Driver not found");
}

Notes and traps: normalize input and driver names consistently; decide how to handle duplicate names (show all matches or ask for additional identifier); update or rebuild the index if depots/drivers change; prefer StringComparison.OrdinalIgnoreCase for comparisons; guard against null driver names. To keep the menu clean, call a single LookupDriver method from the case branch so the switch only routes to the search logic.

Looks like you will need to search by name for a driver in your list of depots. There's a few ways to do this - are you familiar with LINQ?

var FindDriverDepots = from x in mydepots
                       where (x.drivers.FindAll(y => y.name == "Steven").Count != 0)
                       select x;

This will give you a list of depots that contain drivers with the name "Steven"


Or are you asking how to get the user input (ie Console.Readline() )?

Edit: This will likely run faster in a long list of depots/drivers

var FindDriverDepots = mydepots.FindAll(x => x.drivers.Find(y => y.name == "Steven") != null);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.