Posts

Showing posts from November 11, 2018

LINQ: Get all objects with a matching string property from an incoming IEnumerable in a performant way

Image
up vote 0 down vote favorite I just saw this piece of code and asked myself how it can be improved to reduce the amount of queries. Tried a few LINQ statements but could not find an answer. public static Dictionary<string, Computer> GetComputer(IEnumerable<string> workStations) { var dict = new Dictionary<string, Computer>(); using (var db = new ComputerContext()) { foreach (var workStation in workStations) { var t = db.Computers.FirstOrDefault(o => o.Id.Equals(workStation)); if (!dict.ContainsKey(workStation)) { dict.Add(workStation, t); } } return dict; } } When trying someting li...