使用 Datacontext 的 InvalidOperationException

发布于 2025-01-05 20:24:43 字数 1112 浏览 1 评论 0原文

当我运行此命令时,我收到 InvalidOperationException(它显示“无法确定属性名称”)。我查过网络但没有找到解决方案。它发生在 foreach (contacts 中的 var c) 行。

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));
        }

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

I'm getting an InvalidOperationException when I run this (it says "cannot determine the attribute name"). I have checked the net but haven't found a solution. It occurs at the foreach (var c in contacts) line.

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));
        }

Let me know if you have any ideas. Thank you.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

徒留西风 2025-01-12 20:24:43

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

This exception occurred on execution of you query, when iterate on contacts context try execute your query from sql. I think this error occurred because of your context model is not mapped correctly to your database, may be different table name or property.

韬韬不绝 2025-01-12 20:24:43

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

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

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

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

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

I suspect c.parentcustomerid is an id to a related object. It does not have a property called name that you are trying to access.

You want to access the actual Customer object that "parentcustomerid" is pointing to if you want to get the parentcustomer's name.

If what you want is the account id (guessing from the acct property) then just change

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

for

acct = c.parentcustomerid == null ? "" : c.parentcustomerid
夜光 2025-01-12 20:24:43
where c != null

是问题。您要在哪个字段检查 null c.parentcustomerid !=null? 您无法对实体进行 null 检查。

此外,Id 的名称属性将适用于使用 LINQ to Crm 的查找

where c != null

Is the issue. What field are you checking for null c.parentcustomerid !=null? You can't do a null check on the entity.

Also, the name property off Id will work on lookups using LINQ to Crm

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文