如何在C#中读取实体对象
每当我尝试从对象获取特定值时,我的编辑器都会抛出错误:'对象不包含{变量名称}的方法'
。这是到目前为止我的代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我解决了这个问题。我必须用正确的对象名称将对象施放在“ AliasedValue”对象中,这使我可以拔出名称变量。在这种情况下,看起来“ entityReference”在“ AliasedValue”对象内部。这是最终为我工作的代码行:
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:
一个愚蠢的修复方法是简单地创建一个返回 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