gpt4 book ai didi

c# - 选择所有类型的员工,然后选择特定员工

转载 作者:太空宇宙 更新时间:2023-11-03 12:05:38 25 4
gpt4 key购买 nike

这是一种家庭作业,所以有人可以指导我(而不是回答)如何组合这两个 LINQ 查询。这是问题

Update the PayrollSytem by using LINQ to map the List in to a list of anonymous objects in which each object contains an Employee’s name and earning. When a BasePlusCommissionEmployee is encountered, give a 10% base-salary increase without modifying the original BasePlusCommissionEmployee object. Display the names and earnings.

到目前为止我有这个

var model1 = employees.OfType<BasePlusCommissionEmployee>().Select(x => new
{
x.FirstName,
x.LastName,
Increased_salary=x.BaseSalary*=1.10M
});

var model = employees.Select(x => new
{
x.FirstName,
x.LastName,
earning = x.Earnings()
});

foreach (var item in model)
{
Console.WriteLine(item);
}

类的UML图是UML Diagram

所以查询单独工作正常,但有什么方法可以使它成为一个查询。如果需要任何其他代码,我会提供。

最佳答案

您可以在匿名类中使用三元运算符简单地检查该员工是否为基础加佣金员工。

employees.Select(x => new { 
FirstName = x.FirstName,
LastName = x.LastName,
Salary = x is BasePlusCommissionEmployee ? (x as BasePlusCommissionEmployee).BaseSalary * 1.1M : x.Earnings()
}).ToList();

关于c# - 选择所有类型的员工,然后选择特定员工,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55157414/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com