used list to loopup for company name but its very slow, any recommedation?

Posted by Nirmeshptl under C# category on | Points: 40 | Views : 2250
below my code is very slow and has been reported to occasionally throw an exception. wants to know will use of dictionary or ienumerable make any difference?

// Will find the address of the company with the given name.
public string FindAddress(List<Company> companyList, string name)
foreach (Company company in companyList)
if (name.Trim().ToUpper() == company.Name.ToUpper())
return company.Address;

return null;

Comments or Responses

Posted by: Ndebata on: 10/4/2011 Level:Starter | Status: [Member] | Points: 10
Hi Nirmeshptl,
What is the number of items in your list.
If you are using .net 3.5 or above, you can try this inside your function and see ant improvements.
var _cmp=companyList.FirstOrDefault(c=>c.Name.Equals(name, StringComparison.InvariantCultureIgnoreCase));

return (_cmp!=null)?_cmp.Adress:null;

Ideally you could have posted this in forum section. :)

Posted by: T.saravanan on: 10/5/2011 Level:Silver | Status: [Member] [MVP] | Points: 10
Hi Nirmeshptl,

Kindly ask your question in our forum section.. then only you get lot of suggestions from other viewers..
for your reference...

@Ndebata thanks for your info...

Login to post response