Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/google-apps-script/6.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Linq查询返回对象引用异常_C#_Linq_Dynamics Crm 4_Crm_Object Reference - Fatal编程技术网

C# Linq查询返回对象引用异常

C# Linq查询返回对象引用异常,c#,linq,dynamics-crm-4,crm,object-reference,C#,Linq,Dynamics Crm 4,Crm,Object Reference,我看了一些关于Linq代码中出现的异常的帖子,但是没有一篇有帮助。我知道这意味着我在某处引用了一个空值,但我已经设置了一个if语句,它应该可以防止错误 我试图做的是查询CRM数据库,但每个查询的联系人只返回三条信息。异常发生在foreach行。这是我的代码(不是我的全部): 类程序 { 私有静态列表accs=新列表(); 私有静态列表cnts=新列表(); 静态void Main(字符串[]参数) { 如果(!CRMConnectionHelper.Authenticate())引发新异常(“C

我看了一些关于Linq代码中出现的异常的帖子,但是没有一篇有帮助。我知道这意味着我在某处引用了一个空值,但我已经设置了一个if语句,它应该可以防止错误

我试图做的是查询CRM数据库,但每个查询的联系人只返回三条信息。异常发生在foreach行。这是我的代码(不是我的全部):

类程序
{
私有静态列表accs=新列表();
私有静态列表cnts=新列表();
静态void Main(字符串[]参数)
{
如果(!CRMConnectionHelper.Authenticate())引发新异常(“CRM服务器上的身份验证未成功”);
WriteLine(“CRM服务器上的身份验证成功。”);
GetAllAccounts();
GetActiveContacts();
QueryDB();
}
私有静态void QueryDB()
{
var m=碳纳米管中的碳
选择新的
{
acct=c.ParentAccount.name,
last=c.Contact.lastname,
first=c.Contact.firstname
};
列表行=新列表();
尝试
{
foreach(var c in m)**此处例外**
{
如果(c!=null)
{
//string sub=c1.first.PadRight(10).Substring(0,3);//此处为对象引用ex。
Add(string.Format(“{0}\t{1}\t{2}”,c.acct,c.last,c.first));
控制台写入线(c.acct);
系统线程线程睡眠(100);
}
其他的
{
Console.WriteLine(“c为空。继续”);
继续;
}
}
WriteLine(“将列表内容写入.txt…”);
System.IO.File.writeAllines(@“C:\Documents and Settings\paldrich\Desktop\lines1.txt”,lines.ToArray());
控制台。WriteLine(“完成。按ENTER键退出”);
Console.ReadLine();
}
捕获(例外情况除外)
{
WriteLine(String.Format(“错误:{0}”,ex));
}   
}
私有静态void GetAllAccounts()
{
ColumnSet colsAcc=newcolumnset{Attributes=newstring[]{“accountid”、“name”、“statecode”};
QueryExpression accountQuery=newqueryexpression{EntityName=EntityName.account.ToString(),ColumnSet=colsAcc};
BusinessEntityCollection accounts=CRMConnectionHelper.crmService.RetrieveMultiple(accountQuery);
WriteLine(String.Format(“帐户总数{0}”,accounts.BusinessEntities.Length));
对于(inti=0;i

如果你有任何见解,请告诉我。多谢各位

让我们看看这个查询:

var m = from c in cnts
        select new
        {
            acct = c.ParentAccount.name,
            last = c.Contact.lastname,
            first = c.Contact.firstname
        };
此查询返回的任何元素都不会为null。所以你绝对不需要这个:

foreach (var c in m)
{
    if (c != null)
new{…}
表达式从不返回null

但是,如果
c.ParentAccount
为null,或者
c.Contact
为null,则可以从查询中获得异常

您的代码不清楚可能是哪种情况,但您可以将查询更改为:

var m = from c in cnts
        select new
        {
            acct = c.ParentAccount == null ? "" : c.ParentAccount.name,
            last = c.Contact == null ? "" : c.Contact.lastname,
            first = c.Contact == null ? "" : c.Contact.firstname
        };
如果
cnts
包含任何空引用,则
c
本身也有可能在此处为空。您可以轻松忽略所有这些元素:

var m = from c in cnts
        where c != null
        select new
        {
            acct = c.ParentAccount == null ? "" : c.ParentAccount.name,
            last = c.Contact == null ? "" : c.Contact.lastname,
            first = c.Contact == null ? "" : c.Contact.firstname
        };

。。。但是最好确保集合没有任何空元素作为开始。

@Hogan:No-
Select
wil
var m = from c in cnts
        where c != null
        select new
        {
            acct = c.ParentAccount == null ? "" : c.ParentAccount.name,
            last = c.Contact == null ? "" : c.Contact.lastname,
            first = c.Contact == null ? "" : c.Contact.firstname
        };