Refactor selective item by highest create date or update date I have to check for the Highest Date from CostRepository whether it is create date or edit date then i have to get value in that field. edit date can be null. i don't know how to fix the codesmell.

var allCostsInThisPo = CostRepository
                       .GetAll()
                       .WhereInPo(po.po_surr_key);

if (allCostsInThisPo.Any())
{
    var costWithMaxCreatedDate = allCostsInThisPo.OrderByDescending(item =&gt; item.create_date).First();
    var allEditedCosts = allCostsInThisPo.Where(item=&gt;item.update_date != null);


    if (allEditedCosts.Any())
    {
        var costWithMaxEditedDate = allEditedCosts.OrderByDescending(item =&gt; item.update_date).First();

        if (costWithMaxCreatedDate.create_date &gt; costWithMaxEditedDate.update_date)
        {
            history.AllocationInfo = BuilkConstant.LogHistorySetting.GetAllocationHistoryLogDisplay(costWithMaxCreatedDate.CreatedByUser.user_fullname, costWithMaxCreatedDate.create_date);
        }
        else
        {
            history.AllocationInfo = BuilkConstant.LogHistorySetting.GetAllocationHistoryLogDisplay(costWithMaxEditedDate.EditedByUser.user_fullname, costWithMaxEditedDate.update_date.Value);  
        }

    }
    else
    {
        history.AllocationInfo = BuilkConstant.LogHistorySetting.GetAllocationHistoryLogDisplay(costWithMaxCreatedDate.CreatedByUser.user_fullname, costWithMaxCreatedDate.create_date);  
    }

}
You could try and make LINQ do more, to me it is more readable but it may not be to your style.

var allCostsInThisPo = CostRepository
                       .GetAll()
                       .WhereInPo(po.po_surr_key);

if (allCostsInThisPo.Any())
{    
    var costWithMaxCreatedDate = allCostsInThisPo.OrderByDescending(item =&gt; item.create_date)
                                                        .Select(c =&gt; new { Name = c.CreatedByUser.user_fullname, Date = c.create_date})
                                                        .First();

    // if there is a max edited date greated than the cost created date then this will
    // be populated, otherwise will be null. 
    var costWithMaxEditedDate = allCostsInThisPo.Where(item =&gt; item.update_date.HasValue &amp;&amp; item.update_date.Value &gt; costWithMaxCreatedDate.Date)
                                                          .OrderByDescending(c =&gt; item.update_date.Value)       
                                                          .Select(c =&gt; new { Name = c.EditedByUser.user_fullname, Date = c.update_date.Value})                                                                                                  
                                                          .FirstOrDefault();

    var maxCost = costWithMaxEditedDate ?? costWithMaxCreatedDate;

    history.AllocationInfo = BuilkConstant.LogHistorySetting.GetAllocationHistoryLogDisplay(maxCost.Name, maxCost.Date); 
}