Property Mapping with Reflection efficiency I have an object that I am populating via mapping and lookup with Reflection:

this.SearchResults = (from a in response.postings
                          select new SearchResponseModel
                          {
                              Id = a.id,
                              TimeStampDate = a.timestampDate,
                              Body = a.body,
                              Title = a.heading,
                              Status = a.status,
                              State = a.state,
                              Language = a.language,
                              Currency = a.currency,
                              CategoryGroup = a.category_group,
                              Source = a.source,
                              ExternalId = a.external_id,
                              ExternalUrl = a.external_url,
                              Price = a.price,
                              Location = PopulateLocation(a.location)
                          }
                          ).ToList();


And the method that does the mapping:

 private static List&lt;LocationLookupModel&gt; PopulateLocation(Location location)
{
    List&lt;LocationLookupModel&gt; allLocations = new List&lt;LocationLookupModel&gt;();
    if (HttpContext.Current.Session["LocationModel"] == null)
    {

        HttpContext.Current.Session["LocationModel"] = allLocations = new LocationModel().LocationList;
    }
    else
    {
        allLocations = (List&lt;LocationLookupModel&gt;)HttpContext.Current.Session["LocationModel"];
    }
    List&lt;LocationLookupModel&gt; modelList = new List&lt;LocationLookupModel&gt;();

    foreach (PropertyInfo propertyInfo in location.GetType().GetProperties())
    {
        var value = propertyInfo.GetValue(location);
        if (value != null)
        {
            LocationLookupModel model = (from a in allLocations
                                         where a.Code == propertyInfo.GetValue(location).ToString()
                                         select a).FirstOrDefault();
            if (model != null)
            {
                modelList.Add(model);
            }
        }
    }

    return modelList;
}


The issue that I run into is that an allLocations object has about 70k records (it represents a list of location lookup values for countries, states, zipcodes, etc), and populating about 100 instances of SearchResponseModel takes about 20 seconds. This is far too long for a UI call, and I have not been able to find a way to make it faster. I understand I am basically doing 3 nested loops (calling helper method for each population, looping over reflected properties, and finally the LINQ call over 70k records) so there are some time efficiency issues, but I am a bit lost on what tricks I can use to make this process more efficient.

Here is a screenshot of the location object:



Here is a screenshot of one of the allLocation objects in the list:
Well to start with reflection is incredibly slow.  And second I'm not even sure why you're using reflection to begin with since you have the strongly typed Location.  Don't use reflection.  I don't think you need to compare every single property.  Pick only the ones that could possibly match.

If you insist on using reflection here's a faster impl:

private static readonly locationProperties = typeof(Location).GetProperties();

foreach (PropertyInfo propertyInfo in locationProperties)
{
    var value = propertyInfo.GetValue(location);
    if (value != null)
    {
        LocationLookupModel model = allLocations.FirstOrDefault(a.Code == value.ToString());
        if (model != null)
        {
            modelList.Add(model);
        }
    }
}


Without reflection:

private static List&lt;LocationLookupModel&gt; PopulateLocation(Location location)
{
    List&lt;LocationLookupModel&gt; allLocations = new List&lt;LocationLookupModel&gt;();
    if (HttpContext.Current.Session["LocationModel"] == null)
        HttpContext.Current.Session["LocationModel"] = allLocations = new LocationModel().LocationList;
    else
        allLocations = List&lt;LocationLookupModel&gt;)HttpContext.Current.Session["LocationModel"];
    return allLocations.Select(x =&gt; x.Code == location.city || x.Code == location.country || x.Code == location.county || x.Code == location.locality || x.Code == location.metro || x.Code == location.region || x.Code == location.state || x.Code == location.zipcode).ToList();
}