在对象中转换匿名类型并检索一个字段
我使用C# asp.net4。
我有一种用匿名类型(字段:Title、CategoryId)填充转发器的方法,在转发器内我还放置了一个标签:
var parentCategories = from c in context.CmsCategories
where c.CategoryNodeLevel == 1
select new { c.Title, c.CategoryId };
uxRepeter.DataSource = parentCategories;
uxRepeter.DataBind();
我需要在转发器事件 ItemDataBound 上更改转发器内每个标签的文本属性,
protected void uxRepeter_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
HyperLink link = (HyperLink)e.Item.FindControl("uxLabel");
uxLabel.Text = // How to do here!!!!!!!!
}
因此我需要设置属性对于 Label.Text 使用 e.Item (或更好的方法,如果有的话)。
我的问题是我无法转换 e.Item(匿名类型字段标题)并将其设置为我的标签的文本属性。
我知道匿名类型只能转换为对象类型,但就我而言,我的匿名类型具有 Title 和 CategoryId 字段。
我的问题:
如何投射和检索我感兴趣的字段?感谢您抽出时间来讨论这个问题?
编辑: 我收到一些错误:
Unable to cast object of type '<>f__AnonymousType0`2[System.String,System.Int32]' to type 'System.String'.
I use C# asp.net4.
I have a method to populate a Repeater with Anonymous Types (Fields: Title, CategoryId), inside the Repeater I also placed a Label:
var parentCategories = from c in context.CmsCategories
where c.CategoryNodeLevel == 1
select new { c.Title, c.CategoryId };
uxRepeter.DataSource = parentCategories;
uxRepeter.DataBind();
I need to change Text Properties for each label inside my Repeater on Repeater Event ItemDataBound
protected void uxRepeter_ItemDataBound(object sender, RepeaterItemEventArgs e)
{
HyperLink link = (HyperLink)e.Item.FindControl("uxLabel");
uxLabel.Text = // How to do here!!!!!!!!
}
So I need set the properties for Label.Text using e.Item (or a better way if any).
My problem I'm not able to CAST the e.Item (Anonymous type Field Title) and set it as Text Propriety for my Label.
I understand Anonymous Type can be casted to only Object Type, but in my case my Anonymous Type has Title and CategoryId Fields.
My question:
How to cast and retrieve the field with I'm interested? Thanks for your time on this?
EDIT:
SOME ERROR I RECEIVE:
Unable to cast object of type '<>f__AnonymousType0`2[System.String,System.Int32]' to type 'System.String'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
约瑟夫提出的选择是好的,但是有一种可怕的方法可以做到这一点。它有点脆弱,因为它依赖于您在两个地方以完全相同的方式指定匿名类型。我们开始:
然后:
编辑:进一步的皱纹 - 两个匿名类型创建表达式必须位于同一个程序集(项目)中。抱歉之前忘记提及这一点。
The options Joseph presents are good ones, but there is a horrible way you can do this. It's somewhat fragile, as it relies on you specifying the anonymous type in exactly the same way in two places. Here we go:
Then:
EDIT: Further wrinkle - the two anonymous type creation expressions have to be in the same assembly (project). Sorry for forgetting to mention that before now.
您无法将匿名类型转换为任何类型,因为您实际上没有可以将其转换为的类型,正如您基本上已经指出的那样。
所以你确实有两个选择。
示例 1:
示例 2:(我也认为您是最后一行你的意思是分配链接变量)
You can't cast the anonymous type to anything because you literally have no type to cast it to, as you've basically pointed out already.
So you really have two options.
Example 1:
Example 2: (also I think you're last line you meant to assign the link var)
在这种情况下,您可以使用
dynamic
。我认为代码是:You can use a
dynamic
in this case. I think the code would be:您不能直接转换为
(typeof(new { Title = "", CategoryID = 0 }))
吗?Can't you just cast to
(typeof(new { Title = "", CategoryID = 0 }))
?