如何在C#中读取实体对象

发布于 2025-01-18 19:11:44 字数 1234 浏览 1 评论 0原文

每当我尝试从对象获取特定值时,我的编辑器都会抛出错误:'对象不包含{变量名称}的方法'。这是到目前为止我的代码:

EntityCollection accounts = service.RetrieveMultiple(new FetchExpression(fetchXml));
            foreach (var c in accounts.Entities)
            {
                var workOrderNum = c.GetAttributeValue<AliasedValue>("wo_workOrderNumber");
                var workOrderCustomer  = c.GetAttributeValue<AliasedValue>("wo_customer");

                # This line succeeds 
                Console.WriteLine("Name: {0}", workOrderNum != null ? (string)workOrderNum.Value : "");
                # This line fails because 'Object does not contain a definition for name'
                Console.WriteLine("Name: {0}", workOrderCustomer != null ? (string)workOrderCustomer.Value.Name : ""); 
            }

当我调试代码时,我看到变量 workOrderCustomer 中有一个 name 变量: workOrderCustomer Entity Object.png

而不是执行 workOrderCustomer.Value.Name我尝试过 workOrderCustomer.Value['Name']workOrderCustomer.Value.['Name'] 以及其他一些组合均无济于事。关于如何从 workOrderCustomer 对象获取 name 变量有什么想法吗?

感谢您的帮助!

Whenever I try to get a specific value from an object my editor throws the error: 'Object does not contain method for {variable name}'. Here is my code so far:

EntityCollection accounts = service.RetrieveMultiple(new FetchExpression(fetchXml));
            foreach (var c in accounts.Entities)
            {
                var workOrderNum = c.GetAttributeValue<AliasedValue>("wo_workOrderNumber");
                var workOrderCustomer  = c.GetAttributeValue<AliasedValue>("wo_customer");

                # This line succeeds 
                Console.WriteLine("Name: {0}", workOrderNum != null ? (string)workOrderNum.Value : "");
                # This line fails because 'Object does not contain a definition for name'
                Console.WriteLine("Name: {0}", workOrderCustomer != null ? (string)workOrderCustomer.Value.Name : ""); 
            }

When I debugged the code I saw that the variable workOrderCustomer has a name variable within it:
workOrderCustomer Entity Object.png

Instead of doing workOrderCustomer.Value.Name i've tried workOrderCustomer.Value['Name'], workOrderCustomer.Value.['Name'] as well as a few other combinations to no avail. Any idea on how I can get the name variable from the workOrderCustomer object?

Appreciate the help!

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

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

发布评论

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

评论(2

北方。的韩爷 2025-01-25 19:11:44

我解决了这个问题。我必须用正确的对象名称将对象施放在“ AliasedValue”对象中,这使我可以拔出名称变量。在这种情况下,看起来“ entityReference”在“ AliasedValue”对象内部。这是最终为我工作的代码行:

#'(AliasedValue)c["wo_customer"]).Value' gets the EntityReference Object
var Name = ((EntityReference)((AliasedValue)c["wo_customer"]).Value).Name;

I solved the issue. I had to cast the object inside the 'AliasedValue' object with the correct object name that allowed me to pull the name variable. In this case it looks like an object 'EntityReference' was inside the 'AliasedValue' object. Here's the line of code that ended up working for me:

#'(AliasedValue)c["wo_customer"]).Value' gets the EntityReference Object
var Name = ((EntityReference)((AliasedValue)c["wo_customer"]).Value).Name;
饮湿 2025-01-25 19:11:44

一个愚蠢的修复方法是简单地创建一个返回 workOrderCustomer 中的名称的方法,然后在控制台 writeline 中调用该方法

a dumb fix would be to simply create a method that return the name in workOrderCustomer and simply call that method in your console writeline

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