gpt4 book ai didi

c# - 带Where 子句的Linq 左连接

转载 作者:行者123 更新时间:2023-11-29 13:17:35 24 4
gpt4 key购买 nike

我正在使用带有 MySql 的 Entity Framework 5 执行此查询。

var employeeDetails = (from em in entities.employeemasters.AsEnumerable()
join sf in entities.salaryfitments.AsEnumerable()
on em.empID equals sf.empID into emsf
from x in emsf
where (x.edCode.ToString().Trim().Equals(txtEDCode.Text)
&& x.edCode != "SYS001")
select new { em, x });

where (x.edCode.ToString().Trim().Equals(txtEDCode.Text) 检查是否为该员工存储了任何收入/扣除额,如果是,我可以能够得到金额数字。

我希望查询返回所有员工,如果他们没有与 txtEDCode.Text 匹配的特定收入/扣除额,则返回默认值。

我无法将 .DefaultIfEmpty() 放在 where (x.edCode.ToString().Trim().Equals(txtEDCode.Text)

之后

我应该怎样做才能得到合适的结果?

最佳答案

我不会返回整个实体,而是创建一个仅包含我感兴趣的字段的新对象,并使用 ternary if例如,在 select 语句中提供默认值。

select new { 
name = x.Name,
salary = x.Salary,
code = string.IsNullOrEmpty(x) ? "Blah" : x
}

关于c# - 带Where 子句的Linq 左连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21279741/

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