Assignment statement using lamdba expression where property is passed as a string

Posted by Nidabp under LINQ on 6/1/2017 | Points: 10 | Views : 259 | Status : [Member] | Replies : 1
Hi,

Employee class

public class employee
{
public DateTime? Date1 { get; set; }
public DateTime? Date2 { get; set; }
public DateTime? dueDate { get; set; }
public string dueDateExpression { get; set; }

}

List<employee> employees = new List<employee>();
employee emp1 = new employee() { Date1 = Convert.ToDateTime("05-12-2017"), Date2 = Convert.ToDateTime("05-16-2017"), dueDateExpression = "Date1 (50)" };
employee emp2 = new employee() { Date1 = Convert.ToDateTime("05-10-2017"), Date2 = Convert.ToDateTime("05-05-2017"), dueDateExpression = "Date2(50)" };
employee emp3 = new employee() { Date1 = Convert.ToDateTime("05-18-2017"), dueDateExpression = "Date2(50)" };

employees.Add(emp1); employees.Add(emp2); employees.Add(emp3);

Need to evaluate each duedateexpression for each above record.

For the first record, the lamdba expression can be given as

.Select(i => { i.dueDate = (i.Date1.HasValue ? i.Date1.Value.AddDays(50) : (DateTime?)null); return i; }).ToList();

The Date1 and Date2 fields must change in the expression based on the dueDateexpression.

Need to write a lamdba expression to evaluate each object based on the expression in the dueDateExpression field and assign it to dueDate field.

There are number of employee records each having an expression.

Thank you.




Responses

Posted by: Jitendrasoft09 on: 6/10/2017 [Member] [MVP] Starter | Points: 25

Up
0
Down
Try it with below code where dueDate compares with date1 and date2 and assign to it.
.Where(i => { i.dueDate = (i.Date1.HasValue ? i.Date1.Value.AddDays(50) : (DateTime?)null); return i; }).ToList().Select(i => { i.dueDate = (i.Date2.HasValue ? i.Date2.Value.AddDays(50) : (DateTime?)null); return i; }).ToList();


Jitendra Kumar
If my post helps you, plz mark as an answer.

Nidabp, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response