Linq“无效的匿名类型成员声明符”当使用来自不同类的属性时 [英] Linq "invalid anonymous type member declarator" when using property from different class
问题描述
我希望从XML文件中获取信息的类知道另一个名为config的类。
这是我想要填充XML信息的模型类。
如果我使用本机属性(例如用户名),我可以使用我的代码。
但是具有模型类的属性(例如config.mailAddress)它不会工作,即使它是一个常规的字符串属性,与原生的'用户名'相同。
另外,如果我将用户名声明为this.username,它也有相同的错误消息
Quote:错误1无效的匿名类型成员声明符。必须使用成员分配,简单名称或成员访问来声明匿名类型成员。
我尝试过: < br $>
configFile = XDocument.Load(configFilePath);
var v = 来自 e in configFile.Descendants(xmlName)
select new
{
username = e.Element(username).Value, // native,works
this .username = e.Element(username).Value, // native,不起作用
config.MailAddress = e.Element(mailAddress).Value // obv not native,抛出指定的错误
};
MailAddress = e.Element ....
而不是。
The class where I would like to get my information from an XML file knows another class named 'config'.
This is the model-class I'd like to fill with the XML information.
If I use a native property (eg username), I can use my code.
But with the property of the model-class (eg config.mailAddress) it wont work, even tho it's a regular string property, same as the native 'username' one.
Also, if I declare username as this.username, it also has the same error message
Quote:Error 1 Invalid anonymous type member declarator. Anonymous type members must be declared with a member assignment, simple name or member access.
What I have tried:
configFile=XDocument.Load(configFilePath);
var v = from e in configFile.Descendants(xmlName)
select new
{
username = e.Element(username).Value, //native, works
this.username = e.Element(username).Value, //native, does not work
config.MailAddress=e.Element(mailAddress).Value //obv not native, throws the stated error
};
The new { } notation is creating a new anonymous type, such that result.username is what you've defined it as. You can't have config.MailAddress as a property of an anonymous type, it makes no sense. Just use
MailAddress = e.Element....
instead.
这篇关于Linq“无效的匿名类型成员声明符”当使用来自不同类的属性时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!