在实体框架中使用LINQ Contains时出现NullReference异常 [英] NullReference exception when using LINQ Contains with Entity Framework

查看:233
本文介绍了在实体框架中使用LINQ Contains时出现NullReference异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在一个项目中,我将Linq转换为SQL(我承认我理解得很差)到Linq转换为Entity,并且遇到了一种奇怪的情况.当对在Linq to SQL设计器中生成的类进行评估时,下面的代码正确返回结果,但对Entity Framework实体运行时不起作用.特别是,如果Or子句中的任何属性为null,则该过程会因Null Reference异常而中断.我已经尝试过一些技术来检查null属性,然后说,传递一个我不能在SearchTerm中知道的值来对此进行短路,但是它们只会导致LINQ以我不知道的方式返回该实体的所有结果也不是很了解.

I'm working on a project where I'm converting Linq to SQL (which I admittedly very poorly understand) to Linq to Entity and have ran into an odd situation. The code below correctly returns results when evaluated against a class generated in the Linq to SQL designer but does not work when run against an Entity Framework entity. Specifically if any of the properties in the Or clauses are null, the process breaks with a Null Reference exception. I've tried techniques to check for null property and then say, pass a value I know can't be in the SearchTerm to short circuit around this but they just result in this LINQ returning all results from the entity in a way that I don't really understand either.

        Return (From values In Location.getData _
                Where (values.LocName.Contains(SearchTerm) _
                Or values.Address.Contains(SearchTerm) _
                Or values.Address2.Contains(SearchTerm) _
                Or values.City.Contains(SearchTerm) _
                Or values.State.Contains(SearchTerm) _
                Or values.QIM.Contains(SearchTerm) _
                Or values.MacID.Contains(SearchTerm) _
                Or values.Phone.Contains(SearchTerm) _
                Or values.PrimServ.Contains(SearchTerm) _
                Or values.Zip.Contains(SearchTerm) _
                Or values.Area.Contains(SearchTerm) _
                Or values.Type.Contains(SearchTerm)
                ) And (values.Status <> "Closed" Or status_parm = "Closed")
            Select values)

您可能会猜到,实体类Location上的getData方法基本上是使用DbContext.SqlQuery将查询构造到数据库中,然后使用此LINQ来过滤结果.我知道有几个问题使它不理想,但是有一些原因导致在开始转换之前以这种方式编写的原因很难更改.

As you can probably guess the getData method on the entity class Location is basically using DbContext.SqlQuery to construct a query into the database and then this LINQ is being used to filter the results. I'm aware of several issues with this that make it less than ideal, but there are reasons it was written this way before I started the conversion that aren't easily changed.

推荐答案

所以我最终得到的答案是使用ToLower

So the answer I finally got working was to use ToLower

SearchTerm = SearchTerm.ToLower

Return (From values In Location.getData _
Where (values.LocName IsNot Nothing And Also values.LocName.ToLower.Contains(SearchTerm)

我仍然想了解两件事:

  1. 为什么使用LINQ to Entity时需要检查是否为null,而在LINQ to SQL中它却没有问题并且从未遇到NullReference异常?

  1. Why is a check for null required when using LINQ to Entity where in LINQ to SQL it runs without issue and never hits a NullReference exception?

为什么LINQ是.LINQ对SQL不区分大小写,但是在这里?我已经100%肯定地使用LINQ to SQL验证了相同的代码不会抛出NullReference异常,并且不需要处理.包含为区分大小写的函数(因此SearchTerm的任何大小写都将与结果的任何大小写匹配隐含地.)

Why is LINQ's .Contains not case sensitive in LINQ to SQL but it is here? I've verified with 100% certainty this same code using LINQ to SQL does not throw NullReference exceptions ever, and does not require dealing with .Contains as a case sensitive function (so any casing of the SearchTerm will match to any casing of the result implicitly.)

这篇关于在实体框架中使用LINQ Contains时出现NullReference异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆