1

当我运行它时,我得到了一个 InvalidOperationException(它说“无法确定属性名称”)。我已经检查了网络,但没有找到解决方案。它发生在 foreach (var c in contacts) 行。

DataContext ctx = new DataContext("CrmConnection");

        var contacts = from c in ctx.contacts
                       where c != null
                       select new
                       {
                           acct = c.parentcustomerid == null ? "" : c.parentcustomerid.name,
                           last = c.lastname == null ? "" : c.lastname,
                           first = c.firstname == null ? "" : c.firstname
                       };

        List<string> lines = new List<string>();

        try
        {
            foreach (var c in contacts) *ex*
            {
                Console.WriteLine(c.acct);
                Console.ReadLine();
                lines.Add(string.Format("{0}\t{1}\t{2}", c.acct, c.last, c.first));
                Console.WriteLine(c.acct);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(String.Format("Error: {0}", ex));
        }

如果您有任何想法,请告诉我。谢谢你。

4

3 回答 3

0
where c != null

是问题。您正在检查哪个字段是否为空c.parentcustomerid !=null? 您不能对实体进行空检查。

此外,关闭 Id 的 name 属性将适用于使用 LINQ to Crm 的查找

于 2013-04-19T16:12:14.320 回答
0

我怀疑 c.parentcustomerid 是相关对象的 id。它没有您尝试访问的名为 name 的属性。

如果您想获取父客户的名称,您想访问“parentcustomerid”指向的实际客户对象。

如果您想要的是帐户 ID(从 acct 属性猜测),那么只需更改

acct = c.parentcustomerid == null ? "" : c.parentcustomerid.name

为了

acct = c.parentcustomerid == null ? "" : c.parentcustomerid
于 2012-02-16T00:30:20.180 回答
0

此异常发生在执行您的查询时,当迭代联系人上下文时尝试从 sql 执行您的查询。我认为发生此错误是因为您的上下文模型未正确映射到您的数据库,可能是不同的表名或属性。

于 2012-02-16T00:31:51.940 回答