【问题标题】:Object Reference Not Set to an Instance within LINQ to SQL Query对象引用未设置为 LINQ to SQL 查询中的实例
【发布时间】:2011-09-25 20:53:18
【问题描述】:

我在使用 LINQ to SQL 查询时遇到问题,从服务器返回的数据可能为空,因此我在查询中设置了 if 语句,但查询仍然抛出异常。

这是查询代码的简化版

var a = from b in db.branches
        where (b.Location != null) ?
        (
            (Query.Location == null) ?
                true :
                //The following line causes the exception to be thrown
                object.Equals(Query.Location.ToLower() , b.Location.ToLower())
        ) : 
        (
            (Query.Location == null) ?
                true :
                false
       )
       select b;

如果搜索词“位置”为空,那么我不想按位置过滤,但如果它不为空,那么我必须检查行中的值是否为空,因为某些条目有一个空位置。

在我添加比较行之前,代码运行良好。为了到达比较行,Query.Location 和 b.Location 都不能为空,因此代码不应该失败。

知道可能是什么问题吗?

谢谢。

编辑

如果我从 object.equals 调用中删除 .toLower() ,则查询运行正常,无论查询处于何种情况,它也都能正常工作。

var a = from b in db.branches
        where (b.Location != null) ?
        (
            (Query.Location == null) ?
                true :
                //The following line causes the exception to be thrown
                object.Equals(Query.Location , b.Location)
        ) : 
        (
            (Query.Location == null) ?
                true :
                false
       )
       select b;

【问题讨论】:

  • 如果b.Location 为null 而Query.Location 不是,是否需要不选择行?
  • 如果b.Location 为空且Query.Location 为空,是否需要选择所有行?
  • 如果 b.location 为 null 并且 query.location 不是,则不返回它 如果 b.location 为 null 并且 query.location 为 null 然后返回

标签: c# .net linq-to-sql exception-handling nullreferenceexception


【解决方案1】:

我不想确定这里出了什么问题,但我认为您实际上可以通过将代码拆分为两个不同的查询来显着简化代码:

public void Search(SearchTerms Query)
{
    var queryWithLocation = db.branches.Where(b =>
          Query.Location.Equals(b.Location, StringComparison.OrdinalIgnoreCase);

    var query = Query.Location != null ? queryWithLocation : db.branches;
}

我改变了Equals 的执行方式——这是我更喜欢执行不区分大小写搜索的方式;你必须看看它是否适用于 LINQ to SQL。

【讨论】:

  • @abatishchev:如果 Query.Location 为空,您认为我在哪里执行查询?调用Where 实际上并没有执行查询...
  • 我可以将其拆分,但位置是 3 个可能为空的搜索词中的 1 个。所以我需要很多查询,每个可能的组合1个。例如queryWithLocation、queryWithX、queryWithLocationAndX
  • @Midimatt:不,使用组合。从第一个查询构建第二个查询,等等。
【解决方案2】:

试试这个;

public void Search(SearchTerms Query)
{
var a = from b in db.branches
        where (b.Location != null) ?
        (
            (Query.Location == null) ?
            true
            :
            //The following line causes the exception to be thrown
            Query.Location.ToLower() == b.Location.ToLower()

        )
        : 
        (
            (Query.Location == null) ?
            true
            :
            false
       )
       select b
}

我相信object 可能导致了 NullReference

【讨论】:

  • 您是否附加了一个调试器来确定“什么”为空?
  • 我正在通过 vs2010 运行它并单步执行代码,但我看不到什么是 null。
【解决方案3】:
from b in db.branches
let location = b.Location
where location != null ?
    b.Location.Equals(b.Location, Query.Location ?? b.Location, StringComparison.OrdinalIgnoreCase) : // if Query.Location is null then select all
    false // to select nothing in this case
select b;

【讨论】:

    猜你喜欢
    • 2013-09-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多