本文介绍了使用lamdba表达式的赋值语句,其中属性作为字符串传递的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 员工类别

公职人员
    {
      公开DateTime? Date1 {get;放; }
      公开DateTime? Date2 {get;放; }
      公开DateTime? dueDate {获取;放; }
      公共字符串dueDateExpression {get;放; }

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

  }

列表< employee>员工=新列表< employee>();
雇员emp1 =新雇员(){Date1 = Convert.ToDateTime("05-12-2017"),Date2 = Convert.ToDateTime("05-16-2017"),dueDateExpression ="Date1(50)"; };
          雇员emp2 =新雇员(){Date1 = Convert.ToDateTime("05-10-2017"),Date2 = Convert.ToDateTime("05-05-2017"),dueDateExpression ="Date2(50)"; };
          雇员emp3 =新雇员(){Date1 = Convert.ToDateTime("05-18-2017"),dueDateExpression ="Date2(50)" };

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)" };

          员工.添加(emp1);员工.添加(emp2); employee.Add(emp3);

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

需要评估上述每个记录的每个duedate表达式.

Need to evaluate each duedateexpression for each above record.

对于第一个记录,lamdba表达式可以表示为

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);返回i;}).ToList();

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

Date1和Date2字段必须根据DueDateexpression在表达式中进行更改.

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

需要编写一个lamdba表达式,以基于dueDateExpression字段中的表达式评估每个对象,并将其分配给dueDate字段.

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.

谢谢.

推荐答案

https://social.msdn.microsoft.com/Forums/zh-CN/31ac96da-415e-424b-9e1f-6aec86c4c3ae/how-to-con-convert-string-into-systemlinqexpressionsexpression-in-c?forum=csharplanguage

https://social.msdn.microsoft.com/Forums/en-US/31ac96da-415e-424b-9e1f-6aec86c4c3ae/how-to-convert-string-into-systemlinqexpressionsexpression-in-c?forum=csharplanguage

谢谢

病毒式


这篇关于使用lamdba表达式的赋值语句,其中属性作为字符串传递的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 11:08