Home > Enterprise >  I want to modify the data in my object using Linq
I want to modify the data in my object using Linq

Time:10-07

var todate = Filters.Where(it => it.Value == "ApplicationDateToSearch").Select(it =>
                {
                    if (DateTime.TryParse(it.Description, out DateTime ConvertedToDate))
                    {
                        it.Description = ConvertedToDate.AddHours(23).AddMinutes(59).AddSeconds(59).ToString();
                    }
                })?.FirstOrDefault();

Visual Studio doesn't seem to like this.

List<EmployeeRole> Filters

public class EmployeeRole
{         
    public String Description { get; set; }
    public String Value { get; set; }
    public int IntValue { get; set; } 
}

CodePudding user response:

The 'select' statement is used to select the properties you can't modify in that, so you have to use the 'ForEach' statement for that. But 'ForEach' statement doesn't have any return type so after that you can use FirstOrDefault

var x = Filters.Where(i => i.Value == "ApplicationDateToSearch").ToList();
x.ForEach(i => i.Description = DateTime.TryParse(i.Description, out DateTime ConvertedToDate) ?                             ConvertedToDate.AddHours(23).AddMinutes(59).AddSeconds(59).ToString() : string.Empty);
var todate = x.FirstOrDefault();
  • Related