简体   繁体   English

为什么LINQ无法转换string.IsNullOrWhiteSpace()?

[英]Why can LINQ not translate string.IsNullOrWhiteSpace()?

Can someone explain why this compiles but creates an exception at runtime because it is not able to translate to sql/entities correctly? 有人可以解释为什么它可以编译但在运行时创建异常,因为它无法正确转换为sql / entities吗? Trying to gain understanding of what is happening behind the scenes. 试图了解幕后发生的事情。

 var data = (from e in db.Employees
            join egp in db.EmployeeGrievanceMappings on e.EmployeeID equals egp.EmployeeID
            join et in db.EmployeeTypes on egp.EmployeeTypeID equals et.EmployeeTypeID
                    where et.EmployeeTypeName == employeeeType
            select new GrievantStewardBO()
            {
                Id = e.EmployeeADID,
                EmployeeID = e.EmployeeID,
                EmployeeTypeID = egp.EmployeeTypeID,
                GrievanceID = egp.GrievanceID,
                EmployeeGrievanceID = egp.EmployeeGrievanceID,
                text =  string.IsNullOrWhiteSpace( e.EmployeeLastName) ? e.EmployeeFirstName : e.EmployeeLastName + "," + e.EmployeeFirstName,
                FirstName = e.EmployeeFirstName,
                LastName = e.EmployeeLastName

            }).
        ToList();

I know I can write this and have it work correctly, I just wanted to gain some understanding for similar run-ins with LINQ. 我知道我可以编写并使其正常运行,我只是想对LINQ的类似磨合有所了解。

text =   e.EmployeeLastName == null || e.EmployeeLastName.Trim() == "" ? e.EmployeeFirstName : e.EmployeeLastName + "," + e.EmployeeFirstName

Related question: Why does Trim() translate correctly in LINQ? 相关问题:为什么Trim()在LINQ中正确翻译?

You are using "Linq to Entites", likely either Entity Framework or Linq2Sql. 您正在使用“实体的Linq”,可能是Entity Framework或Linq2Sql。 When using Linq to Entites your linq statement is turned in to a expression, parsed, then is converted in to a SQL query. 当使用Linq Entities时,您的linq语句将被转换为一个表达式,进行解析,然后转换为SQL查询。 The problem you are having is as simple as the person who wrote the "parse" step did not support string.IsNullOrWhiteSpace in the parser so it does not know how to turn that function in to a SQL string. 您遇到的问题很简单,就像编写“ parse”步骤的人员不支持string.IsNullOrWhiteSpace ,它不知道如何将函数转换为SQL字符串。

You must either use the long form like you showed or update to a newer version of your library that supports it. 您必须使用显示的长格式,或者更新到支持它的库的较新版本。 It appears that the version on GitHub of Entity Framework 6 does IsNullOrEmpty , perhaps you could create a new method of IsNullOrWhitespace and do a request to get it merged in. 看来Entity Framework 6的 GitHub上的版本执行IsNullOrEmpty ,也许您可​​以创建IsNullOrWhitespace的新方法并提出将其合并的请求。

The LINQ you have there is an expression which gets translated into SQL. 您拥有的LINQ有一个表达式 ,可以将其转换为SQL。 Somebody has to evaluate the expression and create the resulting SQL. 有人必须评估表达式并创建结果SQL。 This process does not know what to do with IsNullOrWhiteSpace() , hence it will fail. 此过程不知道该如何处理IsNullOrWhiteSpace() ,因此它将失败。

Why? 为什么? Nobody wrote code to handle this specific method in the LINQ provider you are using. 没有人在您使用的LINQ提供程序中编写任何代码来处理此特定方法。 Method calls are not translated to SQL (or whatever the provider translates to) by some procedure. 某些过程不会将方法调用转换为SQL(或提供程序转换为任何内容)。 Method calls are handled on a case by case basis and the common methods are hardcoded into the provider. 方法调用将根据具体情况进行处理,而通用方法则被硬编码到提供程序中。 If nobody wrote code to handle this method the method does not work. 如果没有人编写处理此方法的代码,则该方法无效。

On another note why didn't they handle this specific method? 另一方面,他们为什么不处理这种特定方法? Probably they were either reusing code from LINQ to SQL for their generation code or they wanted to work with .NET framework 3.5 and this method was added in 4.0 可能他们是在将LINQ的代码重用到SQL以生成代码,或者他们想使用.NET Framework 3.5,并且此方法已在4.0中添加

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LINQ 表达式中的 String.IsNullOrWhiteSpace - String.IsNullOrWhiteSpace in LINQ Expression .NET string.IsNullOrWhiteSpace实现 - .NET string.IsNullOrWhiteSpace implementation string.IsNullOrWhiteSpace()和string.IsNullOrEmpty()中的NullReferenceException - NullReferenceException in string.IsNullOrWhiteSpace() and string.IsNullOrEmpty() C#string.IsNullOrWhiteSpace(“\\ t”)== true - C# string.IsNullOrWhiteSpace(“\t”) == true string.IsNullOrEmpty(string)与string.IsNullOrWhiteSpace(string) - string.IsNullOrEmpty(string) vs. string.IsNullOrWhiteSpace(string) string.IsNullOrEmpty & string.IsNullOrWhiteSpace 为空字符串返回 false - string.IsNullOrEmpty & string.IsNullOrWhiteSpace return false for empty string Java等价于c#String.IsNullOrEmpty()和String.IsNullOrWhiteSpace() - Java equivalent of c# String.IsNullOrEmpty() and String.IsNullOrWhiteSpace() .Net 3.5使用代码协定实现String.IsNullOrWhitespace - .Net 3.5 Implementation of String.IsNullOrWhitespace with Code Contracts string.IsNullOrEmpty(myString)或string.IsNullOrWhiteSpace(myString)是否违反了SRP规则? - string.IsNullOrEmpty(myString) or string.IsNullOrWhiteSpace(myString) is not violating SRP Rule? string.IsNullOrEmpty(myString.Trim()) 与 string.IsNullOrWhiteSpace(myString) - string.IsNullOrEmpty(myString.Trim()) vs string.IsNullOrWhiteSpace(myString)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM