Linq查询“对象引用未设置为对象的实例". [英] Linq query "Object reference not set to an instance of an object"

查看:77
本文介绍了Linq查询“对象引用未设置为对象的实例".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个简单的Web服务方法,用于查询通过JavaScript查询的XML文档(存储在HTTP缓存中).当我为aff参数传递一个完全有效的数据项时,GetCitiesForAffiliate()方法在选择新"行上引发NullReferenceException.对于aff参数中的其他数据,它可以正常工作.即使使用相同的aff参数导致另一种方法轰炸,另一种方法也可以正常工作.

I have 2 simple webservice methods that query an XML document (stored in HTTP cache) that I am querying via javascript. The GetCitiesForAffiliate() method is throwing a NullReferenceException on the "select new" line when I pass in one perfectly valid item of data for the aff parameter. It works fine for other data in the aff parameter. The other method also works fine, even with the same aff parameter that causes the other method to bomb out.

我刚刚验证了查询的XML.当我传入一个不存在的aff时,这两种方法都只会返回一个空的json字符串,这是可以的.我应该怎么看可能是错误的?

I just validated the XML that I am querying. Both methods just return an empty json string when I pass in an aff that doesn't exist, which is OK. What should I look at that might be wrong?

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetCitiesForAffiliate(string aff)
    {
        LocationService loc = new LocationService();
        var query = (from center in loc.centersXml.Descendants("Center")
                     where center.Element("ServiceArea").Value.Equals(aff)
                     select new {
                         City = center.Element("City").Value
                     }).Distinct().OrderBy(x => x.City);

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(query);

        return json;
    }

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetCentersForAffiliateCity(string aff, string city)
    {
        LocationService loc = new LocationService();
        var query = (from center in loc.centersXml.Descendants("Center")
                     where center.Element("ServiceArea").Value.Equals(aff) && center.Element("City").Value.Equals(city)
                     select new { 
                         ID = center.Element("ID").Value,
                         Name = center.Element("Name").Value 
                     }).Distinct().OrderBy(x => x.Name);

        JavaScriptSerializer serializer = new JavaScriptSerializer();
        string json = serializer.Serialize(query);

        return json;
    }

推荐答案

如果某些节点缺少City元素,则可以根据需要的输出选择一些选项:

If some of your nodes are missing a City element, you have some choices, depending on what output you want:

1)添加检查以查看City元素是否为null:

1) Add a check to see if the City element is null:

City = (center.Element("City") == null ? null : center.Element("City").Value)

2)添加where子句以忽略具有空城市的元素:

2) Add where clause to ignore elements with a null city:

where center.Element("ServiceArea").Value.Equals(aff) 
    and center.Element("City") != null

这篇关于Linq查询“对象引用未设置为对象的实例".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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