我今天面对这种情况,无法在网上找到我想要的。店内变量linq
看看这段代码,
myCollection.Select(g => new ReportLine
{
cds = (sectors.FirstOrDefault(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE)) != null ?
sectors.FirstOrDefault(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE)).USER != null?
sectors.FirstOrDefault(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE)).USER.USER_FIRSTNAME : "": "")
+ " " +
(sectors.FirstOrDefault(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE)) != null ?
sectors.Where(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE)).FirstOrDefault().USER != null?
sectors.Where(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE)).FirstOrDefault().USER.LASTNAME : "" : "")
});
基本上“MyCollection的”是一种深深的类的列表,并申请进入它提取的姓氏和名字,并把它们在其他类而如果检查有没有空值。
君不见实现,我查了同样的事情,6次:
sectors.FirstOrDefault(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE))
(可能是痛苦的性能)
难道一个方式中存在“存储”在LINQ表达式中的价值?是这样的:
myCollection.Select(g => new ReportLine
{
cds = ((var tmp =sectors.FirstOrDefault(s => s.SECT_CODE.Equals(g.Contract.Station.Sector.SECT_CODE))) != null ?
tmp.USER != null?
tmp.USER.USER_FIRSTNAME + "" + tmp.USER.LASTNAME: "": "")
});
很确定EF无法正确翻译。 – Servy
@Servy,为什么不呢?那是标准的linq?你的意思是因为它不能查找'合约',然后'站'和'部门'?如果你问我那不是上述问题的linq,而是EF,如果使用的话,并且他在表达式中有一个真正的嵌套结构。 –