在C#中,我需要创建一个函数,该函数返回一个对象,该对象可能包含以下内容:
{ itemID: "A1", itemDesc: "apple" }
或者
{ groupID: "G1", groupDesc: "group1" } ..etc
// Please don't be concerned about why don't create another function for this,
// or why don't consider 'ID' instead.
我很好奇。原因很简单,在JavaScript中,您可以在不预定义对象的情况下为任何键分配对象。
但是,当涉及到C#时,我感到困惑。
这种情况可能吗?
编辑:
抱歉,我应该更清楚地陈述我的问题(所以它可能不是“重复的”)
我的函数实际上返回一个类,如下所示:
public class MyClass
{
public int myKey {get;set;}
public List<customClass> listRecord {get;set;}
}
< p > customClass
是我所提到的动态 "对象"。请注意,记录是从 LINQ 查询的。
感谢您的建议,但我似乎无法做到这一点:
var result = (from a in db.MyTable
select new System.Dynamic.ExpandoObject()
{
....
我无法选择新的动态
ExpandoObject
都不适合你。 - Peter Duniho